How to print integers in the form of Pascal triangle using C?

Pascal's triangle is a triangular arrangement of integers where each number is the sum of the two numbers directly above it. This mathematical structure has many applications in combinatorics, algebra, and probability theory.

Syntax

// Using factorial approach
int factorial(int n);
int binomialCoefficient = factorial(row) / (factorial(col) * factorial(row - col));

// Using iterative approach  
int coefficient = 1;
coefficient = coefficient * (row - col) / (col + 1);

How Pascal Triangle Works

Pascal's triangle follows these rules −

  • The first and last elements of each row are always 1
  • Each interior element equals the sum of the two elements above it
  • Row n contains n+1 elements
  • Each element represents a binomial coefficient C(n,k)
Pascal Triangle Structure 1 1 1 1 2 1 1 3 3 1 Each number = sum of two numbers above

Method 1: Using Factorial Approach

This method calculates each element using the binomial coefficient formula C(n,k) = n!/(k!(n-k)!) −

#include <stdio.h>

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

int main() {
    int rows = 5;
    printf("Pascal Triangle with %d rows:<br>", rows);
    
    for (int i = 0; i < rows; i++) {
        /* Print spaces for triangle alignment */
        for (int j = 0; j < rows - i - 1; j++) {
            printf("  ");
        }
        
        /* Calculate and print binomial coefficients */
        for (int j = 0; j <= i; j++) {
            int coefficient = factorial(i) / (factorial(j) * factorial(i - j));
            printf("%3d ", coefficient);
        }
        printf("<br>");
    }
    return 0;
}
Pascal Triangle with 5 rows:
        1 
      1   1 
    1   2   1 
  1   3   3   1 
1   4   6   4   1 

Method 2: Using Iterative Approach

This optimized method calculates each element using the previous element in the same row without computing factorials −

#include <stdio.h>

int main() {
    int rows = 6;
    printf("Pascal Triangle with %d rows:<br>", rows);
    
    for (int row = 0; row < rows; row++) {
        int coefficient = 1;
        
        /* Print spaces for alignment */
        for (int space = 0; space < rows - row - 1; space++) {
            printf("  ");
        }
        
        /* Print coefficients for current row */
        for (int col = 0; col <= row; col++) {
            printf("%3d ", coefficient);
            coefficient = coefficient * (row - col) / (col + 1);
        }
        printf("<br>");
    }
    return 0;
}
Pascal Triangle with 6 rows:
          1 
        1   1 
      1   2   1 
    1   3   3   1 
  1   4   6   4   1 
1   5  10  10   5   1 

Comparison

Method Time Complexity Space Complexity Advantages
Factorial O(n³) O(1) Simple to understand
Iterative O(n²) O(1) More efficient, no large factorials

Key Points

  • The iterative method is preferred for large triangles as it avoids computing large factorials
  • Proper spacing is important for visual triangle alignment
  • Each row represents the coefficients of binomial expansion (a+b)?

Conclusion

Pascal's triangle can be efficiently generated using either factorial calculations or iterative coefficient computation. The iterative approach is more efficient and recommended for larger triangles due to its better time complexity.

Updated on: 2026-03-15T13:42:10+05:30

421 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements