 # C Programming Code Examples

## C > Pyramid Patterns Code Examples

### Pascal's Triangle Printing In C

``` Pascal's Triangle Printing In C Pascal's triangle is one of the classic example taught to engineering students. It has many interpretations. One of the famous one is its use with bionomial equations. All values outside the triangle are considered zero (0). The first row is 0 1 0 whereas only 1 acquire a space in pascal's triangle, 0s are invisible. Second row is acquired by adding (0+1) and (1+0). The output is sandwiched between two zeroes. The process continues till the required level is achieved. Pascal's triangle can be derived using binomial theorem. We can use combinations and factorials to achieve this. #include <stdio.h> int factorial(int n) { int f; for(f = 1; n > 1; n--) f *= n; return f; } int ncr(int n,int r) { return factorial(n) / ( factorial(n-r) * factorial(r) ); } int main() { int n, x, j; n = 5; for(x = 0; x <= n; x++) { for(j = 0; j <= n-x; j++) printf(" "); for(j = 0; j <= x; j++) printf(" %3d", ncr(x, j)); printf("\n"); } return 0; } procedure pascals_triangle FOR x = 0 to N DO FOR J = 0 to N-1 DO PRINT " " END FOR FOR J = 0 to x DO PRINT nCr(x,j) END FOR PRINT NEWLINE END FOR end procedure ```