Created
March 15, 2017 14:48
-
-
Save thinkphp/89e42c92e453008ee94934dc8fdc0839 to your computer and use it in GitHub Desktop.
Pascal's Triangle in C made easy with Dynamic Programming.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Blaise Pascal's Triangle | |
*/ | |
#include <stdio.h> | |
#define MAXN 20 | |
int main() { | |
int n, | |
i, | |
j, | |
k, | |
sp; | |
unsigned long long pascal[ MAXN ][MAXN ]; | |
printf("Enter the rows of Pascal's triangle ->>> n="); | |
scanf("%d",&n); | |
pascal[0][0] = 1; | |
for(i = 1; i <= n; ++i) { | |
for(j = 0; j <= i; ++j) { | |
if( 0 == j || i == j) pascal[i][j] = 1; | |
else | |
pascal[i][j] = pascal[i-1][j] + pascal[i-1][j-1]; | |
} | |
} | |
sp = n; | |
for(i = 0; i <= n; ++i) { | |
for(k = sp; k>=0; k--) printf(" "); | |
sp--; | |
for(j = 0; j <= i; ++j) { | |
printf("%d ", pascal[i][j]); | |
} | |
printf("\n"); | |
} | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment