SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

C Program to print Pascal Triangle

28 April 2015

Simple C Program to print Pascal Triangle without using functions or arrays.

Question:

Write a C program to input the number of rows and print Pascal's Triangle.

pascal triangle

Pascal's Triangle: Pascal's Triangle is a triangular number pattern in which the first and last value of every row is 1. Each inner value is obtained by adding the two values just above it.

Example: If the number of rows is 5, the output should be:

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

Algorithm:

Step 1: Start.

Step 2: Declare integer variables i, j, k, n and space, and declare a long integer variable c.

Step 3: Input the number of rows in n.

Step 4: Store n in space so that leading blanks can be printed before each row.

Step 5: Run an outer loop with i from 0 to n - 1.

Step 6: At the start of each row, set c = 1.

Step 7: Print leading spaces from space down to 0, then decrease space by 1.

Step 8: Run an inner loop with j from 0 to i.

Step 9: Print the current value of c.

Step 10: Calculate the next value using c = c * (i - j) / (j + 1).

Step 11: After the inner loop ends, move to the next line.

Step 12: Stop.

Explanation:

Pascal's Triangle can be printed without using an array because every row can be generated directly from the previous value in the same row. The first value of every row is always 1, so the variable c is set to 1 before printing a new row. The outer loop controls the row number. When i is 0, the program prints only one value. When i is 1, it prints two values, and so on. Therefore the inner loop runs from 0 to i, giving exactly the required number of terms in each row.

The important part of the program is the formula c = c * (i - j) / (j + 1). This formula gives the next binomial coefficient in the current row. For example, in row 4, the values are 1, 4, 6, 4 and 1. Starting with 1, the formula calculates the next value using only the current value, the row number and the column number. Since the multiplication is done before division, the result remains correct for the integer values in Pascal's Triangle.

The variable space is used only for formatting. It prints leading blanks before each row so the output appears in a triangular shape. After each row, space is decreased, making the next row begin slightly earlier. The logic of the triangle does not depend on these spaces; they simply make the pattern easier to read on the screen.

C Program:

C
/*
 * Program to print Pascal Triangle
 * @author : www.guideforschool.com
 * @Program Type : C Program
 */

#include <stdio.h>

int main(void)
{
    int i, j, n, k, space;
    long c;

    printf("\nEnter no. of lines : ");
    scanf("%d", &n);

    printf("\n");
    space = n;

    for(i = 0; i < n; i++)
    {
        c = 1;

        // Print leading spaces to shape the triangle.
        for(k = space; k >= 0; k--)
        {
            printf("   ");
        }

        space--;

        // Print values of the current row.
        for(j = 0; j <= i; j++)
        {
            printf("%6ld", c);
            c = c * (i - j) / (j + 1);
        }

        printf("\n");
    }

    return 0;
}

Output:

Plain
Enter no. of lines : 7

                             1
                          1     1
                       1     2     1
                    1     3     3     1
                 1     4     6     4     1
              1     5    10    10     5     1
           1     6    15    20    15     6     1

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →