Posted By:
21 Jun, 2013 9:19 am PDT

Pascal Triangle in C

How can you print Pascal Triangle?
Attachment:None


The Pascal Triangle is as follows :

1

11

121

1331

14641

This is for 5 lines. The number of lines to be printed is decided by the user. We use the function factorial to display the triangle. The code for factorial is as follows.

 

#include <stdio.h>
#include<conio.h>
long factorial(int);
 
int main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");
 
      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
 
   getch();
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for( c = 1 ; c <= n ; c++ )
         result = result*c;
 
   return ( result );
}

 

Please see the video for more details

 

Was it useful?  



Instead of going with the loops , we can also go with arrays . We can consider a big array and we can break the entire array into smaller parts in terms of [i][j] . 

Now to completely understand the working of the code , we need to understand the pascal triangle , which is easily explained in the above video. 

Have a look at the below attached file to see the program of pascal triangle in terms of array.

 

Please have a pen and paper nearby to understand this program. 

draw a big 10*10 matrix and then go according to the program.

Attachment: pascal.docx

Was it useful?  




Please login to reply to this problem.