Skip to content

Commit

Permalink
Merge pull request #221 from taresh18/master
Browse files Browse the repository at this point in the history
Add Pascal Triangle
  • Loading branch information
ephremdeme authored Oct 2, 2020
2 parents e6863b9 + 13a729e commit ac9e851
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Algorithms/PascalTriangle
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// including necessary header files
#include<bits/stdc++.h>
using namespace std;

// function to calculate binomial coefficient
int BinCoef(int n, int k)
{
int result = 1;
if (k > n - k)
{
k = n - k;
}
for (int i = 0; i < k; i++)
{
result *= (n - i);
result /= (i + 1);
}
return result;
}

// function to generate Pascal triangle (of lines n)
void pascal(int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i; j <= n; j++)
{
cout <<" ";
}
for (int j = 0; j <= i; j++)
{
cout << BinCoef(i, j)<<" ";
}
cout << endl;
}
}

// main function
int main()
{
int rows;
cin >> rows;
pascal(rows);
return 0;
}

0 comments on commit ac9e851

Please sign in to comment.