C program to Print Pascal Triangle | C code champ

C program to Print Pascal Triangle | C code champ

6874
15
SHARE

C program to print pascal triangle : In our previous code, we have seen how to draw a pyramid using . Now we will see how to extend our previous logic to print pascal’s triangle using C code. But first of all, we must know what is pascal triangle is ?

(From Wikipedia) In , Pascal’s triangle is a triangular array of the binomial coefficients in a triangle. The rows of Pascal’s triangle are conventionally enumerated starting with row n = 0 at the top. The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the in the adjacent rows. A simple construction of the triangle proceeds in the following manner. On row 0, write only the number 1. Then, to construct the elements of following rows, add the number above and to the left with the number above and to the right to find the new value. If either the number to the right or left is not present, substitute a zero in its place. For example, the first number in the first row is 0 + 1 = 1, whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.

This construction is related to the binomial coefficients by Pascal’s rule, which says that if then for any non-negative integer n and any integer k between 0 and n.

Now let us see how to write a C program of language which takes number of rows as input and then displays Pascal’s triangle.

C program to print Pascal triangle :

```#include<stdio.h>
#include<conio.h>
int main() {

int a, i, j, rows, num = 25, k;
printf("----------------------------------------------------------------------\n");
printf("-------------------made by C code champ ------------------------------\n");
printf("----------------------------------------------------------------------\n");
printf("\n\n\t\t C PROGRAM OF PASCALS TRIANGLE \n");
printf("\n Enter the number of rows you want to show : ");
scanf("%d", &rows);
printf("\n\n\n");
for (i = 0; i < rows; i++) {
for (k = num - 2 * i; k >= 0; k--)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == j) {
a[i][j] = 1;
} else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
printf("%4d", a[i][j]);
}
printf("\n");
}
getch();
}```

We hope that you all have enjoyed the above C code to Print Pascal’s triangle. If you have any issues with above code or logic ask us in form of comments.

SHARE
Previous articleC code to draw pyramid using stars | C code champ
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com