Ques: Write a program to print PASCAL triangle
In pascal triangle we start with
print "1" at the top, and after that continue placing numbers below
it in the for pyramid triangle. Each number in this triangle is the sum of
above two number starting from left.
Suppose we have to print this pascal
triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
ROW 0 = 1;
ROW 1 = ( 0 + 1);
ROW 2 = ( 0 + 1)( 1 + 0);
ROW 3 = ( 0 + 1 )( 1 + 2 )( 2 + 1 )( 1 + 0 );
ROW 4 = ( 0 + 1 )( 1 + 3 )( 3 + 3 )( 3 + 1 )( 1 + 0 );
1 2 1
1 3 3 1
1 4 6 4 1
ROW 0 = 1;
ROW 1 = ( 0 + 1);
ROW 2 = ( 0 + 1)( 1 + 0);
ROW 3 = ( 0 + 1 )( 1 + 2 )( 2 + 1 )( 1 + 0 );
ROW 4 = ( 0 + 1 )( 1 + 3 )( 3 + 3 )( 3 + 1 )( 1 + 0 );
There are lots of way to create Pascal Triangle Two of them are here
Source Code 1:
- using System;
- public class PascleTriangle
- {
- public static void Main()
- {
- int row,i,j,c;
- Console.Write("Enter the no. of row you want to print: ");
- row=Convert.ToInt32(Console.ReadLine());
- for(i=0;i<=row;i++)
- {
- c=1;
- for(j=i; j<=row-1; j++)
- Console.Write(" ");
- for(j=0;j<=i;j++)
- {
- Console.Write("{0} ",c);
- c=(c*(i-j)/(j+1));
- }
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- }
OR
Source Code 2:
- using System;
- public class PascleTriangle
- {
- public static void Main()
- {
- int i,j,row,pas;
- Console.Write("Enter the no. of rows in pascle Triangle: ");
- row=Convert.ToInt32(Console.ReadLine());
- for(i=0; i<=row;i++)
- {
- for(j=0;j<=(row-i);j++)
- Console.Write(" ");
- for(j=0; j<=i; j++)
- {
- pas=calc(i)/(calc(j)*calc(i-j));
- Console.Write("{0} ",pas);
- }
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- public int calc(int num)
- {
- int x, res=1;
- for(x=1;x<=num;x++)
- res=res*x;
- return res;
- }
- }