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

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...