Write a C++ programming code to print Pascal Triangle.

Program Code:
#include<iostream>
long fact(int);
int main()
{
int i, n, c;
cout<<"Enter number of rows to print Pascal triangle : ";
cin>>n;
cout<<endl;
for(i=0; i<n; i++)
{
for(c=0; c<=(n-i-2); c++)
{
cout<<" ";
}
for(c=0; c<=i; c++)
{
cout<<fact(i)/(fact(c)*fact(i-c));
}
cout<<endl;
}
return 0;
}
long fact(int n)
{
int c;
long res=1;
for(c=1; c<=n; c++)
{
res = res*c;
}
return (res);
}


The program output is tested on www.jdoodle.com
Output: 5
       1
     1 1  
    1 2 1
   1 3 3 1
 1 4 6 4 1


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: