Pascal Triangle in C++
Here you will learn to make the program code of Pascal Triangle in C++ programming language.
Pascal Triangle Formula
Formula of Pascal triangle for nth row is given below:
C(n,k) = n! / (k! *(n-k)!)
in which nth is the rows and kth is the element.
Pascal Triangle in C++ using For loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; main() { int i , j , rows , sp , num = 1; cout<<"nnEnter number of rows: "; cin>>rows; for (i = 0; i < rows; i++) { for(sp=1; sp <= rows-i; sp++) cout<<" "; for ( j = 0; j <= i; j++) { if (j == 0 || i == 0) num = 1; else num = num * (i - j + 1) / j; cout<<" "<<num; } cout<<"\n"; } return 0; } |
Output
Enter number of rows: 7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Check out our other C++ programming Examples