-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathPascal’s Triangle.cpp
53 lines (48 loc) · 1.01 KB
/
Pascal’s Triangle.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
#include<iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter the number of rows : ";
cin >> rows;
cout << endl;
for (int i = 0; i < rows; i++)
{
int val = 1;
for (int j = 1; j < (rows - i); j++)
{
cout << " ";
}
for (int k = 0; k <= i; k++)
{
cout << " " << val;
val = val * (i - k) / (k + 1);
}
cout << endl << endl;
}
cout << endl;
return 0;
}
// 2nd Approach with the help of Vector
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cout<<"Enter No. of rows you want";
cin>>n;
vector<vector<int>> pascal(n);
for(int i=0;i<n;i++){
pascal[i].resize(i+1);
pascal[i][0]=pascal[i][i]=1;
for(int j=1;j<i;j++){
pascal[i][j]=pascal[i-1][j]+pascal[i-1][j-1];
}
}
for(auto i:pascal){
for(auto j:i){
cout<<j<<" ";
}
cout<<"\n";
}
return 0;
}