A c program that generates the Pascal's triangle.
#include <stdio.h>
#include <conio.h>
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
{
result = result*c;
}
return result;
}
void main()
{
int i, n, c;
printf("Enter the number of rows: ");
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();
}
Comments
Leave a comment