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
Post A Comment:
0 comments: