C Program - Print Pascal's Pattern Triangle Pyramid

#include <stdio.h>

long factorial(int n) {
    long f = 1;
    for(int i = 1; i <= n; i++)
        f *= i;
    return f;
}

int main() {
    int i, j, space, n = 5;

    for(i = 0; i < n; i++) {
        // Print leading spaces
        for(space = 0; space < n - i - 1; space++)
            printf("  ");

        // Print Pascal's values
        for(j = 0; j <= i; j++) {
            long val = factorial(i) / (factorial(j) * factorial(i - j));
            printf("%4ld", val);
        }
        printf("\n");
    }
    return 0;
}
        1
      1   1
    1   2   1
  1   3   3   1
1   4   6   4   1
Sai Kishore Randhi, author
About the author

Sai Kishore Randhi

Embedded systems, VLSI, and chip design

Sai Kishore writes about embedded systems, VLSI, chip design, and the programming concepts behind modern hardware.

View LinkedIn profile ↗