C Program to Print Pascal Triangle with Output and Explanation
for loops and calculates each value using the formula value = value * (row - col) / (col + 1) inside the loops to print the triangle.Examples
How to Think About It
Algorithm
Code
#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.
Start first row (i=0)
value = 1; print 1; no more columns; move to next line
Start second row (i=1)
value = 1; print 1; calculate next value = 1 * (1-0)/(0+1) = 1; print 1; move to next line
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 print | Value after calculation |
|---|---|---|---|
| 0 | 0 | 1 | N/A |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | N/A |
| 2 | 0 | 1 | 2 |
| 2 | 1 | 2 | 1 |
| 2 | 2 | 1 | N/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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Formula in loop | O(n^2) | O(1) | Efficient and memory-friendly |
| Factorial method | O(n^3) | O(1) | Simple but slow for large n |
| 2D array method | O(n^2) | O(n^2) | Easy to understand, uses more memory |
value = value * (row - col) / (col + 1) inside the loop to efficiently calculate Pascal's triangle values without factorials.