Skip to main content

BEGINNER's TRICKY NUMBER FULL PYRAMID

                             PRINT A NUMBER PYRAMID AS MENTIONED BELOW :



APPROACH
  • The whole pyramid structure can be written in a single nested for loop instead of many separate loops
  • A single nested for loop is used along with two flags that help in creating the pyramid structure.One flag moves towards left by 1 and the other to the right by 1.
  • If the iteration is at the boundary of the triangle (leftTriangleCreater, rightTriangleCreater) , those can print the Ith Iteration value
  • While loop can be used to control the values on the left and right.
  • In the left , The while loop iterates till secondelement != middleNumber
  • In the right, The while loop iterates till rightsecondElement > i

SOLUTION

using System;

public class Test
{
public static void Main()
{
  var number = Console.ReadLine();
  int n = Convert.ToInt32(number);
  Pattern print = new Pattern();
  print.NumberPyramid(n);
}
}


public class Pattern
{
public void NumberPyramid(int n)
{
int totalNoOfIterations = (n*2)-1;
int leftTriangleCreater = n;
int rightTriangleCreater = n;
int middleNumber = 1;
int secondElement;
int rightsecondElement;
for(int i=1;i<=n;i++)
{
secondElement = i+1;
rightsecondElement = middleNumber-1;
for(int j=1;j<=totalNoOfIterations;j++)
{
if(j>=leftTriangleCreater && j<=rightTriangleCreater)
{
    if(j==n)
    {
Console.Write(middleNumber);
    }
    else
    { 
    if(j==leftTriangleCreater || j==rightTriangleCreater)
    {
    Console.Write(i);
    }
    else
   
    if(j<n)
    {
    while(secondElement!=middleNumber)
    {
    Console.Write(secondElement);
    secondElement++;
    }
    }
    else
    {
    while(rightsecondElement>i)
    {
    Console.Write(rightsecondElement);
    rightsecondElement--;
    }
    }
    }
    }
}
else
{
Console.Write(" ");
}
}
leftTriangleCreater--;
rightTriangleCreater++;
middleNumber=middleNumber+2;
Console.WriteLine();
}
}
}

Comments

Popular posts from this blog

Print word pattern - odd letters - beginner's code

1. Print the word with odd letters as P M R A O R G O R R A P M Solution: using System; public class Test { public static void Main() { string text = Console.ReadLine(); int n=text.Length; Pattern print = new Pattern (); print.XPattern(text,n); } } public class Pattern { public void XPattern(string text,int n) { char[] textarray = text.ToCharArray(); int reverseprinter = n-1; int middleLetter = n/2; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j && j!=middleLetter) { Console.Write(textarray[j]); } else if(j==reverseprinter) { Console.Write(textarray[reverseprinter]); reverseprinter--; } else { Console.Write("*"); } } Console.WriteLine(); } } } OUTPUT Please Enter text: INPUT : program p m r a o r g o r r a p m

GRANDSON PROBLEM

https://www.geeksforgeeks.org/zoho-interview-set-1-campus/ Given a two dimensional array of string like <”luke”, “shaw”> <”wayne”, “rooney”> <”rooney”, “ronaldo”> <”shaw”, “rooney”> Where the first string is “child”, second string is “Father”. And given “ronaldo” we have to find his no of grandchildren Here “ronaldo” has 2 grandchildren. So our output should be 2. SOLUTION using System; public class Test { public static void Main() { var noOfInputs = Console.ReadLine(); int noOfInputLines = Convert.ToInt32(noOfInputs); string[,] FatherSonArray= new String[noOfInputLines,noOfInputLines]; for(int father=0;father<noOfInputLines;father++) { for(int son=0;son<2;son++) { FatherSonArray[father,son] = Console.ReadLine(); } }     Console.WriteLine("No of grandchildrens:");     string GrandFather = Console.ReadLine();     int NoOfGrandSons=0;     Console.WriteLine(GrandFather);     s

BEGINNER's ALPHABET PALINDROME

PRINT ALPHABET PALINDROMIC PYRAMID USING C# using System; public class Test {       public static void Main()       {             var number = Console.ReadLine();             int n = Convert.ToInt32(number);             Pattern print = new Pattern();             print.PrintPalindromePattern(n);       } } public class Pattern {       public void PrintPalindromePattern(int n)       {             int dummy = 1;             char letter = 'A';             int reversepalindrome;             char letteraftereachIteration ='A';             char reverseletter ='A';             for(int i=1;i<=n;i++)             {                     reversepalindrome = i-1;                   for(int j=1;j<=dummy;j++)                   {                         if(j<=i)                         {                         Console.Write(letter++);                         }                         else                         {