**Pascal Triangle in C Program – **Pascal’s triangle is a triangular array of the binomial coefficients. Write a function that takes an integer value n as input and prints first n lines of the Pascal’s triangle. Following are the first 6 rows of Pascal’s Triangle. Today, We are going to share with you how to print Pascal Triangle in C Program step by step Guide check it out here. The program code for printing Pascal’s Triangle is a very famous problem in C language. In this post, I have presented codes in C program for Pascal’s triangle.

We have already shared format specifiers in C programming, **Fibonacci Series in C**

#### Pascal Triangle

Pascal triangle is a triangular number pattern named after famous mathematician Blaise Pascal.

For example Pascal triangle with 5 rows.

1 2 3 4 5 | 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 |

### Logic:

Simple formula for getting any term of the Pascal triangle.

Where n is row number and k is term of that row.

## C program to print pascal triangle

`/**`

` `

`* C program to print Pascal triangle up to n rows`

` `

`*/`

`#include <stdio.h>`

`long`

`fact(`

`int`

`n);`

`int`

`main()`

`{`

` `

`int`

`n, k, num, i;`

` `

`long`

`term;`

` `

`printf`

`(`

`"Enter number of rows : "`

`);`

` `

`scanf`

`(`

`"%d"`

`, &num);`

` `

`for`

`(n=0; n<=num; n++)`

` `

`{`

` `

`//Prints 2 spaces`

` `

`for`

`(i=n; i<=num; i++)`

` `

`printf`

`(`

`"%2c"`

`, `

`' '`

`);`

` `

`for`

`(k=0; k<=n; k++)`

` `

`{`

` `

`term = fact(n)/( fact(k) * fact(n-k));`

` `

`printf`

`(`

`"%4ld"`

`, term);`

` `

`}`

` `

`printf`

`(`

`"\n"`

`);`

` `

`}`

` `

`return`

`0;`

`}`

`//Function to calculate factorial`

`long`

`fact(`

`int`

`n)`

`{`

` `

`long`

`factorial = 1;`

` `

`while`

`(n>=1)`

` `

`{`

` `

`factorial *= n;`

` `

`n--;`

` `

`}`

` `

`return`

`factorial;`

`}`

## Leave a Reply