Share on Google+Share on Google+

C Print Pascal Triangle

In this section, you will learn how to display Pascal's triangle.

C Print Pascal Triangle

In this section, you will learn how to display Pascal's triangle. A Pascal's triangle is a geometric arrangement of the binomial coefficients in a triangle.The rows of Pascal's triangle are conventionally enumerated starting with row 0 which is having only the number 1. For other rows, add the number directly above and to the left with the number directly 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 2 in the second row are added to produce the number 3 in the third row. In this way, the rows of the triangle go on infinitely. Here we prompt the user to enter the number of rows to display therefore only five rows will get displayed in the Pascal's triangle.

Here is the code:


void main() {

        int a[15][15], i, j, rows, num = 25, k;
        printf("\n Enter the number of rows:");
        scanf("%d", &rows);
        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]);



Enter the number of rows:5
                                           1   1
                                         1   2  1
                                       1  3   3   1
                                     1  4  6   4  1                         




Posted on: January 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null