0
0
CProgramBeginner · 2 min read

C Program to Print Pascal Triangle with Output and Explanation

A C program to print Pascal's triangle uses nested for loops and calculates each value using the formula value = value * (row - col) / (col + 1) inside the loops to print the triangle.
📋

Examples

Input5
Output1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Input1
Output1
Input0
Output
🧠

How to Think About It

To print Pascal's triangle, think of each row as a list of numbers where each number is the sum of the two numbers above it. Start with 1 at the beginning of each row. Use a loop to build each row by calculating values based on the previous value using multiplication and division to avoid factorials.
📐

Algorithm

1
Get the number of rows to print from the user.
2
For each row from 0 to number of rows - 1:
3
Start with the first value as 1.
4
Print the first value.
5
For each position in the row from 1 to current row number:
6
Calculate the next value using the formula: value = value * (row - col + 1) / col.
7
Print the calculated value.
8
Move to the next line after each row.
💻

Code

c
#include <stdio.h>

int main() {
    int rows, i, j;
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for (i = 0; i < rows; i++) {
        int value = 1;
        for (j = 0; j <= i; j++) {
            printf("%d ", value);
            value = value * (i - j) / (j + 1);
        }
        printf("\n");
    }
    return 0;
}
🔍

Dry Run

Let's trace printing 3 rows of Pascal's triangle through the code.

1

Start first row (i=0)

value = 1; print 1; no more columns; move to next line

2

Start second row (i=1)

value = 1; print 1; calculate next value = 1 * (1-0)/(0+1) = 1; print 1; move to next line

3

Start third row (i=2)

value = 1; print 1; next value = 1 * (2-0)/(0+1) = 2; print 2; next value = 2 * (2-1)/(1+1) = 1; print 1; move to next line

Row (i)Col (j)Value before printValue after calculation
001N/A
1011
111N/A
2012
2121
221N/A
💡

Why This Works

Step 1: Initialize first value

Each row starts with the value 1 because the edges of Pascal's triangle are always 1.

Step 2: Calculate next values

Each next value in the row is calculated from the previous value using value = value * (row - col) / (col + 1) to avoid factorial calculations.

Step 3: Print values row-wise

Values are printed in each row separated by spaces, and a newline is printed after each row to form the triangle shape.

🔄

Alternative Approaches

Using factorial function
c
#include <stdio.h>

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

int main() {
    int rows;
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j <= i; j++) {
            int val = factorial(i) / (factorial(j) * factorial(i - j));
            printf("%d ", val);
        }
        printf("\n");
    }
    return 0;
}
This method uses factorials to calculate binomial coefficients but is less efficient due to repeated factorial calculations.
Using 2D array to build triangle
c
#include <stdio.h>

int main() {
    int rows;
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    int pascal[rows][rows];

    for (int i = 0; i < rows; i++) {
        pascal[i][0] = 1;
        for (int j = 1; j <= i; j++) {
            pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
        }
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j <= i; j++) {
            printf("%d ", pascal[i][j]);
        }
        printf("\n");
    }
    return 0;
}
This approach stores the entire triangle in a 2D array and builds it from the top down, which uses more memory but is easy to understand.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.

Space Complexity

The main approach uses constant extra space O(1) as it calculates values on the fly without storing the entire triangle.

Which Approach is Fastest?

The formula-based approach is faster and uses less memory than the factorial or 2D array methods, which are slower or use more space.

ApproachTimeSpaceBest For
Formula in loopO(n^2)O(1)Efficient and memory-friendly
Factorial methodO(n^3)O(1)Simple but slow for large n
2D array methodO(n^2)O(n^2)Easy to understand, uses more memory
💡
Use the formula value = value * (row - col) / (col + 1) inside the loop to efficiently calculate Pascal's triangle values without factorials.
⚠️
Beginners often forget to update the value inside the inner loop, causing all printed numbers to be 1.