Pascal’s Triangle in C
Higher categoruy: C Language
a. GitHub
#include <stdio.h>
#include <stdlib.h>
/* This source is for contouring every stage of Pascal's Triangle */
int Pascal_Number(int i, int j){ // This function indicates the i-th floor j-th number
if(i == j || j == 1) return 1;
else
return Pascal_Number(i-1, j-1) + Pascal_Number(i-1, j); // (m)C(n) = (m-1)C(n-1) + (m-1)C(n)
} // Not using factorial function can prevent overflowing
int main(int argc, char *argv[]) {
int n;
scanf("%d", &n);
int i, j;
for(i = 1; i <= n; i++){
for(j = 1; j <= i; j++){
printf("%d ", Pascal_Number(i, j));
}
if(i != n) printf("\n");
}
return 0;
}
Input: 2016.02.08 17:52