Print Pascal Triangle C Program

By | December 17, 2015

C Program To Print Pascal Triangle

Learn How To Print Pascal Triangle in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code.

About Pascal’s Triangle

Pascal’s Traingle is named after a famous mathematician Blaise Pascal. Pascal Triangle is an Array of Binomial Co – Efficients in a Triangular Format. Pascal Triangle includes Calculation of Factorial of a Number and then processing the next digit. This Pascal Triangle is generated using For Loop.

Note: This Code to Generate Pascal Triangle in C Programming has been compiled with GNU GCC Compiler and developed with gEdit Editor and Terminal in Linux Ubuntu Terminal Operating System.

Method 1: C Program To Print Pascal Triangle using For Loop

#include <stdio.h>
 
long fact(int);
 
int main()
{
    int i, limit, count;
    printf("\nEnter The Rows To Be Printed:\t");
    scanf("%d", &limit);
    for (i = 0; i < limit; i++)
    {
        for(count = 0; count <= (limit - i - 2); count++)
        {
            printf(" ");
        }           
        for(count = 0; count <= i; count++)
        {
            printf("%ld ", fact(i)/(fact(count) * fact(i - count)));
        }       
        printf("\n");
    }
    return 0;
}
 
long fact(int limit)
{
    int i;
    long fact = 1;
    for(i = 1; i <= limit; i++)
    {
        fact = fact * i;
    }
    return fact;
}

Method 2: Code To Display Pascal Triangle in C Programming using While Loop

#include <stdio.h>
 
long fact(int);
 
int main()
{
    int i, limit, count;
    printf("\nEnter The Rows To Be Printed:\t");
    scanf("%d", &limit);
    i = 0;
    while(i < limit)
    {
        count = 0;
        while(count <= (limit - i - 2))
        {
            printf(" ");
            count++;
        }     
        count = 0;      
        while(count <= i)
        {
            printf("%ld ", fact(i)/(fact(count) * fact(i - count)));
            count++;
        }       
        i++;
        printf("\n");
    }
    return 0;
}
 
long fact(int limit)
{
    int i;
    long fact = 1;
    i = 1;
    while(i <= limit)
    {
        fact = fact * i;
        i++;
    }
    return fact;
}

Output

C Program To Print Pascal Triangle

If you have any doubts or Compilation Errors in this C Program To Print Pascal Triangle, let us know about about it in the Comment Section below.

Let's Discuss