In this tutorial, we will write a program to print pascal triangle in c. Before that, you should have knowledge on the following topic in C.
Pascal’s triangle is one of the important examples that is taught to the student. The number outside Pascal’s triangle is zero (0), which is not visible. It is a pattern where the triangle starts from 1 at the top and below is the addition of the upper level as you can see in the figure below. The addition continues until the required level is reached.
C Program to Print Pascal Triangle
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 | #include <stdio.h> int main() { int rows, i, j, numCoef = 1, gap; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = 0; i < rows; i++) { for (gap = 1; gap <= rows - i; gap++) printf(" "); for (j = 0; j <= i; j++) { if (j == 0 || i == 0) numCoef = 1; else numCoef = numCoef *(i - j + 1) / j; printf("%4d", numCoef); } printf("\n"); } return 0; } |
Output: Pascal’s Triangle in C.
1 2 3 4 5 6 7 | Enter the no. of rows: 6 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 |