In this example you will learn how to display Pascal’s Triangle
The program below displays the pascal triangle of degree N and stores it in a square matrix M of dimension N + 1.
C Code:
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 | #include<stdio.h> #include<stdlib.h> #define N 10 int M[N+1][N+1]; unsigned long fact_recursive (unsigned short number) { if (number == 0) return 1; else return number * fact_recursive(number - 1); } int triangle(int b) { int i,k=1,j; for(i=0;i<b;i++) { for(j=0;j<=i;j++) { k=fact_recursive(i)/(fact_recursive(j)*fact_recursive(i-j)); printf("%4d\t",k); M[i][j]=k; } printf("\n"); } } int main() { triangle(N); return 0; } |
Output: