Skip to content

Commit ac9e851

Browse files
authored
Merge pull request ephremdeme#221 from taresh18/master
Add Pascal Triangle
2 parents e6863b9 + 13a729e commit ac9e851

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Algorithms/PascalTriangle

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// including necessary header files
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
// function to calculate binomial coefficient
6+
int BinCoef(int n, int k)
7+
{
8+
int result = 1;
9+
if (k > n - k)
10+
{
11+
k = n - k;
12+
}
13+
for (int i = 0; i < k; i++)
14+
{
15+
result *= (n - i);
16+
result /= (i + 1);
17+
}
18+
return result;
19+
}
20+
21+
// function to generate Pascal triangle (of lines n)
22+
void pascal(int n)
23+
{
24+
for (int i = 0; i < n; i++)
25+
{
26+
for (int j = i; j <= n; j++)
27+
{
28+
cout <<" ";
29+
}
30+
for (int j = 0; j <= i; j++)
31+
{
32+
cout << BinCoef(i, j)<<" ";
33+
}
34+
cout << endl;
35+
}
36+
}
37+
38+
// main function
39+
int main()
40+
{
41+
int rows;
42+
cin >> rows;
43+
pascal(rows);
44+
return 0;
45+
}

0 commit comments

Comments
 (0)