In this example, you will learn a C++ program to print Pascal’s triangle on the screen. Pascal’s triangle is formed by placing 1 along the right and left edges. After that, each value of the triangle is filled by the sum of the above row’s two values just above the given position. This triangle was named after the French mathematician Blaise Pascal.
Example: C++ program to print Pascal’s triangle
//C++ program to print Pascal triangle
#include<iostream>
using namespace std;
int main()
{
int totalRows,x,y,z,c,place;
cout<<"Enter total rows for pascal triangle: ";
cin>>totalRows;
cout<<"\n\n";
place=totalRows;
for(x=0;x<totalRows;x++){
c=1;
for(y=place;y>=0;y--)
cout<<" ";
place--;
for(z=0;z<=x;z++){
cout<<c<<" ";
c=(c*(x-z)/(z+1));
}
cout<<"\n";
}
return 0;
}