Table of contents
1.
Introduction
2.
What is Pascal's triangle in C?
2.1.
Formula
3.
Pascal's Triangle Pattern
4.
Flowchart of Pascal’s Triangle
5.
C Program to Generate Pascal's Triangle
5.1.
Code Explanation
6.
Frequently Asked Questions
6.1.
Can Pascal's Triangle be generated for any number of rows?
6.2.
Why do we use the formula coef*(i-j+1)/j in the code?
6.3.
Is it necessary to use two loops to create Pascal's Triangle?
6.4.
What is a Pascal-like Triangle?
6.5.
Why is Pascal's Triangle Important?
7.
Conclusion
Last Updated: Feb 18, 2025
Easy

Pascal Triangle Program in C

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pascal's Triangle is a mathematical construct that has intrigued scholars for centuries. Named after the French mathematician Blaise Pascal, this triangle is generated by starting with a single 1 at the top, and then constructing subsequent rows where each number is the sum of the two directly above it. It plays a significant role in combinatorics, algebra, and calculus. 

pascal traingle program in C

In this article, we're going to create a program in the C programming language to generate Pascal's Triangle, delve into the logic behind it, and answer some common questions about the process.

What is Pascal's triangle in C?

Pascal's triangle is a triangular array where each number is the sum of the two numbers directly above it. It starts with a 1 at the top, followed by rows where each row begins and ends with 1. The triangle is widely used in mathematics, especially in combinatorics and probability theory, for calculating binomial coefficients.

Formula

To find the value at position row\text{row}row and column\text{column}column in Pascal's triangle, use the binomial coefficient formula: C(n,k)=n!/ k!(n-k)!​ where n is the row number (starting from 0), and k is the column number (also starting from 0).

Pascal's Triangle Pattern

Pascal’s Triangle is a triangular pattern of numbers where:

  1. The first and last numbers of each row are always 1.
  2. Each inner element is the sum of the two numbers directly above it.
  3. The n-th row represents the binomial coefficients for (a+b)n.

Flowchart of Pascal’s Triangle

Here is a flowchart for generating Pascal's Triangle:

  1. Start
  2. Input the number of rows (n)
  3. Loop through each row (i from 0 to n-1)
    • Print the leading spaces (for formatting)
    • Initialize the first value as 1
    • Loop through each column (j from 0 to i):
      • Compute the value using C(i,j)=i!/ j!(i-j)!​
      • Print the value
  4. Move to the next row
  5. Repeat until all rows are printed
  6. End

Here’s a visual representation of the Pascal’s Triangle flowchart:

		+------------------+  
        |      Start       |  
        +------------------+  
                 |  
                 v  
        +------------------+  
        | Input n (rows)   |  
        +------------------+  
                 |  
                 v  
        +------------------+  
        | Loop i = 0 to n-1|  
        +------------------+  
                 |  
                 v  
        +------------------+  
        | Print spaces     |  
        +------------------+  
                 |  
                 v  
        +------------------+  
        | Set C = 1        |  
        +------------------+  
                 |  
                 v  
        +------------------+  
        | Loop j = 0 to i  |  
        +------------------+  
                 |  
                 v  
        +------------------+  
        | Print C value    |  
        +------------------+  
                 |  
                 v  
        +-----------------------+  
        | Compute next C        |  
        | C = C * (i-j) / (j+1) |  
        +-----------------------+  
                 |  
                 v  
        +------------------+  
        | Move to new line |  
        +------------------+  
                 |  
                 v  
        +------------------+  
        | Repeat for rows  |  
        +------------------+  
                 |  
                 v  
        +------------------+  
        |       End        |  
        +------------------+  

C Program to Generate Pascal's Triangle

Let's start with a basic C program that generates Pascal's Triangle:

#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("  ");


        for(int j=0; j<=i; j++) {
            if (j==0 || i==0)
                coef = 1;
            else
                coef = coef*(i-j+1)/j;


            printf("%4d", coef);
        }
        printf("\n");
    }


    return 0;
}
You can also try this code with Online C Compiler
Run Code

This program asks for the number of rows, then calculates and prints each row of Pascal's Triangle.

Output 

When u enter no. of rows as 5

Output

Code Explanation

The main components of this program are two nested loops: an outer loop to handle the rows and an inner loop to handle the coefficients for each row.

The outer loop runs from 0 to the number of rows. The first inner loop creates spaces, and the second inner loop calculates the coefficients.

The formula coef*(i-j+1)/j is based on the combinatorics principle used in Pascal's Triangle, where each element can be represented as nCr (n choose r). For each new element in a row, we multiply the previous result by (i-j+1)/j.

Frequently Asked Questions

Can Pascal's Triangle be generated for any number of rows?

Yes, theoretically, it can be generated for any number of rows. However, in practical applications, the limit would be your machine's capabilities (memory and processing power).

Why do we use the formula coef*(i-j+1)/j in the code?

This formula represents the combinatorics principle nCr (n choose r), a key idea in constructing Pascal's Triangle.

Is it necessary to use two loops to create Pascal's Triangle?

Yes, you need two loops: one for rows and another for elements within a row. The outer loop handles rows while the inner loop calculates and prints elements in each row.

What is a Pascal-like Triangle?

A Pascal-like triangle is any triangular array following a recursive rule similar to Pascal's Triangle but with different starting values or operations.

Why is Pascal's Triangle Important?

Pascal's Triangle is important in combinatorics, binomial expansions, and probability theory, as it provides binomial coefficients and patterns for solving mathematical and computational problems.

Conclusion

Pascal's Triangle has fascinated mathematicians and computer scientists for its pattern and applications. This article explains how to generate Pascal's Triangle using C and the logic behind it, and it addresses common questions. Hopefully, this exercise has enriched your understanding of both Pascal's Triangle and the art of programming with C. 
Recommended Readings:

Live masterclass