C Program to Print Pascal’s Triangle


Summary: In this programming example, you will learn to print a Pascal’s Triangle using C language.

Pascal’s triangle is a triangular array of numbers in which the first and last number in each row is 1, and each of the other numbers is the sum of the two numbers immediately above it. The rows of the triangle are numbered starting from the top, with the top row being row 0.

For example, a 5-row Pascal’s triangle would look like this:

Pascal Triangle

Here is the source code in C to print the pascal’s triangle:

#include <stdio.h>

int main() {
    int rows, coef = 1, space, i, j;

    // Get the number of rows from the user
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    // Iterate through the rows
    for(i = 0; i < rows; i++) {
        // Print spaces before the coefficients
        for(space = 1; space <= rows-i; space++)
            printf("  ");

        // Print the coefficients
        for(j = 0; j <= i; j++) {
            if (j == 0 || i == 0)
                coef = 1;
                coef = coef*(i-j+1)/j;

            printf("%4d", coef);

        // Move to the next row

    return 0;


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

In the above program, we use nested for loops to print Pascal’s triangle. The outer loop iterates through the rows, while the inner loop iterates through the columns (coefficients) of each row.

The program accepts number of rows from the user using the scanf() function.

Inside loop, we print the spaces before the coefficients to align the triangle correctly.

We calculate the coefficients using the binomial coefficient formula in the innermost if-else statement.

At the end, we print a newline character to move to the next row and complete the program.

Leave a Reply

Your email address will not be published. Required fields are marked *