-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from taresh18/master
Add Pascal Triangle
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |