Pascal Triangle Pattern in C++ language of Numbers
ADVERTISEMENTS
Pascal triangle pattern in c++ language of numbers. In this article, you will learn how to print the pascal triangle pattern in c++ language of numbers.
Source Code
// Pascal Triangle Pattern in C++ language of Numbers
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int r, co = 1, ws, i, j;
// r - denotes the number of rows
// co - denotes the coefficient value
// ws - denotes the white space
cout << "-----Enter the number of rows to print the pattern-----\n";
cin >> r;
cout << "\n-----This is the pascal triangle pattern-----\n\n";
for (i = 0; i < r; i++) {
cout << "\t";
for (ws = 1; ws <= r - i; ws++) {
cout << " ";
}
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
co = 1;
else
co = co * (i - j + 1) / j;
cout << std::setw(4) << co;
}
cout << "\n";
}
return 0;
}
Output
-----Enter the number of rows to print the pattern-----
8
-----This is the pascal triangle pattern-----
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
1 7 21 35 35 21 7 1