How to Print a Pascal’s Triangle Pattern in C

In C, we can print Pascal’s Triangle using nested loops and mathematical computations such as factorials or binomial coefficients.

Pascal’s Triangle is a triangular arrangement of numbers where each number is the sum of the two numbers directly above it in the previous row.

In this tutorial, we will explore multiple ways to generate Pascal’s Triangle using C programming.


Examples to Print Pascal’s Triangle

1. Printing Pascal’s Triangle Using a Simple Nested Loop

In this example, we will use nested loops to print Pascal’s Triangle up to a given number of rows. We will compute the binomial coefficient using a direct calculation inside the loop.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows, coef = 1;
    
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    
    for (int i = 0; i < rows; i++) {
        for (int space = 1; space <= rows - i; space++)
            printf("  "); // Printing spaces for alignment

        for (int j = 0; j <= i; j++) {
            if (j == 0 || i == 0)
                coef = 1;
            else
                coef = coef * (i - j + 1) / j;
            
            printf("%4d", coef); // Printing binomial coefficient
        }
        printf("\n");
    }
    
    return 0;
}

Explanation:

  1. We take user input for the number of rows using scanf().
  2. We use an outer for loop to iterate through rows.
  3. We print leading spaces in an inner loop to format the triangle correctly.
  4. Another for loop calculates binomial coefficients using coef, which follows the formula: C(n, k) = C(n, k-1) x (n – k + 1)/k.
  5. The coefficients are printed in a formatted way to maintain triangular shape.

Output (for 5 rows):

Enter number of rows: 5
             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1

2. Printing Pascal’s Triangle Using a Function for Factorial

In this example, we will use a function to calculate the factorial of a number, which will then be used to compute binomial coefficients for Pascal’s Triangle.

main.c

</>
Copy
#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 space = 1; space <= rows - i; space++)
            printf("  "); 

        for (int j = 0; j <= i; j++) {
            int coef = factorial(i) / (factorial(j) * factorial(i - j));
            printf("%4d", coef);
        }
        printf("\n");
    }
    
    return 0;
}

Explanation:

  1. We define a function factorial() that calculates factorial using a loop.
  2. The main() function asks the user for the number of rows.
  3. Nested loops are used to format and print Pascal’s Triangle.
  4. Binomial coefficients are computed using the formula C(n, k) = n!/(k! x (n-k)).

Output (for 5 rows):

        1
      1   1
    1   2   1
  1   3   3   1
1   4   6   4   1

Conclusion

In this tutorial, we explored different ways to print Pascal’s Triangle in C:

  1. Using nested loops: Directly computing binomial coefficients.
  2. Using a factorial function: Computing binomial coefficients via factorial calculations.

Both methods achieve the same result, but the factorial function approach enhances code reusability.