forked from pawanrajsingh2088/cpp-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal's_triangle.cpp
More file actions
36 lines (31 loc) · 849 Bytes
/
Pascal's_triangle.cpp
File metadata and controls
36 lines (31 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result(numRows);
for (int i = 0; i < numRows; i++) {
result[i] = vector<int>(i + 1, 1); // initialize each row with 1s
for (int j = 1; j < i; j++) {
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
}
}
return result;
}
};
int main() {
Solution s;
int numRows;
cout << "Enter number of rows for Pascal's Triangle: ";
cin >> numRows;
vector<vector<int>> triangle = s.generate(numRows);
cout << "\nPascal's Triangle:\n";
for (const auto& row : triangle) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;
}