Program In C To Obtain Pascal's Triangle

Leave a Comment
C/C++ Program To Get "PASCAL TRIANGLE"- C/C++ Project Code
Program In C To Obtain Pascal's Triangle
/* The given program produces output in terms of PASCAL's Triangle, the user is asked about the number of rows that he wants to print on screen, & the output is displayed. The C/C++ code is in bold & the comments are in normal font */

#include <stdio.h>    //For standard i/o functions
#include <stdlib.h>   //For system("pause")
 

long factorial(int n)    //Defining A Function For Factorial Calculation
{
int c;
long result = 1;

for( c = 1 ; c <= n ; c++ )
result = result*c;

return ( result );
}

 
int main()
{
int i, n, c;

printf("Enter the number of rows in pascal triangle\n");
scanf("%d",&n);  
//getting the number of rows

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");


}
system("pause");
return 0;
}  
//End of the program

0 comments: