Easy Tutorial
❮ C Function Fwrite C Examples ❯

C Exercise Example 61 - Pascal's Triangle

C Language Classic 100 Examples

Title: Print Pascal's Triangle (Request to print 10 rows).

Program Analysis:

The structure is as follows:

1
1    1
1    2    1
1    3    3    1
1    4    6    4    1

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include <stdio.h>

int main()
{
    int i, j;
    int a[10][10];
    printf("\n");
    for(i = 0; i < 10; i++) {
        a[i][0] = 1;
        a[i][i] = 1;
    }
    for(i = 2; i < 10; i++)
        for(j = 1; j < i; j++)
            a[i][j] = a[i-1][j-1] + a[i-1][j];
    for(i = 0; i < 10; i++) {
        for(j = 0; j <= i; j++)
            printf("%5d", a[i][j]);
        printf("\n");
    }
}

The above code outputs the following result:

1
1    1
1    2    1
1    3    3    1
1    4    6    4    1
1    5   10   10    5    1
1    6   15   20   15    6    1
1    7   21   35   35   21    7    1
1    8   28   56   70   56   28    8    1
1    9   36   84  126  126   84   36    9    1

C Language Classic 100 Examples

❮ C Function Fwrite C Examples ❯