-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPattern_41.cpp
103 lines (87 loc) · 1.84 KB
/
Pattern_41.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// // Using Vector
// #include <iostream>
// #include <vector>
// using namespace std;
// vector<vector<int>> PascalTriangle(int n)
// {
// vector<vector<int>> ans(n);
// for (int i = 0; i < n; i++)
// {
// ans[i].resize(i + 1);
// ans[i][0] = ans[i][i] = 1;
// for (int j = 1; j < i; j++)
// {
// ans[i][j] = (ans[i - 1][j] + ans[i - 1][j - 1]);
// }
// }
// return ans;
// }
// int main()
// {
// int n;
// cout << "Enter the number of rows : ";
// cin >> n;
// vector<vector<int>> ans = PascalTriangle(n);
// for (auto i : ans)
// {
// for (int s = 0; s < n - i.size(); s++)
// cout << " ";
// for (auto j : i)
// cout << j << " ";
// cout << endl;
// }
// return 0;
// }
// Using Array
#include <iostream>
using namespace std;
// Using Vector
void PascalTriangle(int n)
{
// Initially all the Value will be initialized to zero
int arr[n][n];
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
arr[i][j] = 0;
}
}
// Creating the Pascal Triangle
for (int i = 0; i < n; i++)
{
arr[i][0] = arr[i][i] = 1;
for (int j = 1; j < i; j++)
{
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
// Printing the Pascal Triangle
for (int i = 0; i < n; i++)
{
// Spaces
for (int j = 0; j < n - i - 1; j++)
cout << " ";
// Values
for (int j = 0; j <= i; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int n;
cout << "Enter the number of rows : ";
cin >> n;
PascalTriangle(n);
return 0;
}
/*
Output
Enter the number of rows : 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/