Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024
Difficulty: Easy

Pascal Triangle Program in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


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, 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.

Pascal's Triangle Structure

Pascal's Triangle has a distinct structure. For instance, the 5th row in the triangle would be: 1 4 6 4 1.

If you observe, the nth row is obtained by adding the (n-1)th row elements. This will serve as a fundamental idea in our programming approach.

Also see - Bit stuffing program in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

A Basic 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: ");

    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;
                coef = coef*(i-j+1)/j;

            printf("%4d", coef);

    return 0;

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


When u enter no. of rows as 5


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.

Also see, Floyd's Triangle in C

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.


Pascal's Triangle has fascinated mathematicians and computer scientists for its pattern and applications. This article has explained how to generate Pascal's Triangle using C, the logic behind it, and addressed common questions. Hopefully, this exercise has enriched your understanding of both Pascal's Triangle and the art of programming with C. As with all skills, the more you practice, the more proficient you will become. So, keep coding and keep exploring!

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Topics covered
Pascal's Triangle Structure
A Basic Program to Generate Pascal's Triangle
Code Explanation
Frequently Asked Questions
Can Pascal's Triangle be generated for any number of rows?
Why do we use the formula coef*(i-j+1)/j in the code?
Is it necessary to use two loops to create Pascal's Triangle?