Skip to content

Update Pascal's Triangle.cpp #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions PATTERN/Pascal's Triangle.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
#include<iostream>

#include<iomanip>
using namespace std;
long fact(long n){
int i, fact = 1;
for(i = n; i>1; i--)
fact *= i;
return fact;//factorial of given number
}
long nCr(long n, long r){
long nume = 1, i;
for(i = n; i>r; i--)
nume *= i;
return long(nume/fact(n-r));//generate result of nCr
}
void genPascalsTriangle(long n){
for(int i = 0; i<n; i++){
for(int j = 0; j<(n-i-1); j++)
cout <<setw(3)<< " ";//printing space to show triangular form
for(int j = 0; j<(i+1); j++)
cout <<setw(3)<< nCr(i, j) <<setw(3)<< " ";
cout << endl;
}
}
main(){
int n;
cin >> n;
genPascalsTriangle(n);
}

int main()
{
int n,i,j;
cin>>n;

int a[n][n];

for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(i==0||j==i||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}