September 30, 2020 # Program to Compute Pascal Triangle

This article describes the program to generate Pascal triangle in c/c++. Let us discuss about the Pascal’s triangle.

What is Pascal’s Triangle?
In mathematics Pascal’s Triangle is one of the most interesting Number Patterns. It is a triangular array of the binomial coefficients.

How to build Pascal triangle?
To build the triangle, start with “1” at the top, and then continue placing numbers below it in a triangular pattern. Each Pascal number is the two numbers above it added together (except for the edges which are all “1”).

0 row = 1
1 row = adding the two numbers above them to the left and the right
= (0+1) , (1+0)
= 1 , 1
2 row = (0+1) , (1+1) , (1+0)
= 1 , 2 , 1
3 row = (0+1), (1+2), (2+1), (1+0)
= 1 , 3 , 3 , 1
4 row = (0+1), (1+3) , (3+3), (3+1), (1+0)
= 1 , 4 , 6 , 4 , 1

C Program to Compute Pascal Triangle

#include
#include
#include
int main(){
clrscr();
int n,q=0,r,x,s=1;

cout<<”            ” This program will show the Pascals Triangle. “”<<endl<<endl;

xx:
cout<<“How many Rows ?. Give a integer number.”<<endl;
cin>>n;

if(n<1) {
cout<<“Negative number is not allow .Please give a positeve number.”<<endl;
goto xx;
}

cout<<endl;
cout<<“Pascals Triangle is :”<<endl;
while(q<n) {
for(r=40-3*q;r>0;–r)
cout<<” “;
for(x=0;x<=q;++x) {
if((x==0)||(q==0))
s=1;
else s=(s*(q-x+1))/x;
cout<<s<<”     “;
}
cout<<endl;
++q;
}

getch();
}

Output
If we run the program it will generate a Pascal Triangle and the output is like that: #### Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

## One thought on “Program to Compute Pascal Triangle”

1. ST. Jons Jardan says:

very nice and useful collections.
Thanks for such a nice article.