C Program to Print Pascal's Pattern Triangle Pyramid - Source Code, Output & Execution Flow

C Program to Print Pascal's Pattern Triangle Pyramid

This C program prints Pascal's triangle using binomial coefficients.

#include <stdio.h>
int main() {
    int rows, coef = 1, space, i, j;
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    for (i = 0; i < rows; i++) {
        for (space = 1; space <= rows - i; space++) printf("  ");
        for (j = 0; j <= i; j++) {
            if (j == 0 || i == 0) coef = 1;
            else coef = coef * (i - j + 1) / j;
            printf("%4d", coef);
        }
        printf("
");
    }
    return 0;
}
Enter number of rows: 5
          1
        1   1
      1   2   1
    1   3   3   1
  1   4   6   4   1

In this program, the input is read first when required, the core logic is applied step by step, and the final result is displayed using printf(). Follow the execution flow beside the code to understand how each statement contributes to the output.

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 ↗