-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathPascal_Triangle.c
More file actions
46 lines (35 loc) · 836 Bytes
/
Pascal_Triangle.c
File metadata and controls
46 lines (35 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Each number in the triangle is the sum of the two numbers directly above it.
we take number of rows as input
And we know that to find any element at particular position (i.e at ith row and cth column) we can use the combination formula nCr.
nCr=factorial(n)/(factorial(r)*factorial(n-r))
*/
#include <stdio.h>
long factorial(int n)
{
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 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");
}
return 0;
}