Search code examples
c#seriespascals-triangle

How can i modify this nested for loop in C#?


I am trying to print out a pyramid/Pascal Triangle that will give output in series of 3. You can see it in the example below.

                               1
                           1   3   1
                       1   3   9   3   1
                   1   3   9   27  9   3   1
               1   3   9   27  81  27  9   3   1
           1   3   9   27  81  243 81  27  9   3  1
      1    3   9   27   81  243 729 243 81  27  9  3  1
  1   3   9   27  81   243 729 2187 729 243 81 27  9  3   1

                                                                                                                 

Instead of getting the above output, I am getting this:

code output

Here is my code:

using System;

namespace ConsoleApp
{
    class PiscalTriangle
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter length : ");
            int num = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < num; i++)
            {
                for (int j = num; j > i; j--)
                {
                    Console.Write("  ");
                }
                int val = 1;
                for (int j = 0; j <= i; j++)
                {
                    Console.Write(val + "   ");
                    val = val * (i - j) / (j + 1);
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

Solution

  • There are three main aspects to be considered for generating the desired ouput:

    • The depth of the pascal tree which is defined with the help of num variable and an iterator i which ranges from 1 to num
    • The elements in each row of the pascal tree are in the sequence 1,3,5,7.. which when correlated with depth of tree is equivalent to 2i-1. Hence the row values iterator j ranges from 1 to 2i-1
    • And finally the value (For example 3rd row, 1 3 9 3 1), can be written in the form of 3 power x, where x increases from 0 to i and then decreases to 0

    By summing up the 3 conditions, we can get our desired output.

    using System;
    
    namespace ConsoleApp
    {
        class PiscalTriangle
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter length : ");
                int num = Convert.ToInt32(Console.ReadLine());
                
                for (int i = 1; i <= num; i++)
                {
                    for (int j = num; j > i; j--)
                    {
                        Console.Write("  ");
                    }
                    var x = 0;
                    for (double j = 1; j <= 2*i-1; j++)
                    {
                        double val = Math.Pow(3, x);
                        Console.Write(val + " ");
                        if(j < i)
                        {
                            x++;
                        }
                        else
                        {
                            x--;
                        }
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        }
    }