//
// main.cpp
// Pascal Triangle
//
// Created by Himanshu on 20/09/21.
//
#include <iostream>
using namespace std;
void printPascalTriangle (int n) {
int pascal[n+1][n+1];
//Base case
pascal[1][1] = 1;
for (int i=0; i<=n; i++) {
for (int j=0; j<=n; j++) {
pascal[i][j] = 0;
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=i; j++) {
// first and last binomial coefficients are
// always 1
if (i == 1 || j == i) {
pascal[i][j] = 1;
} else {
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
}
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=i; j++) {
cout<<pascal[i][j]<<" ";
}
cout<<endl;
}
}
int main() {
int n = 7;
printPascalTriangle (n);
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBQYXNjYWwgVHJpYW5nbGUKLy8KLy8gIENyZWF0ZWQgYnkgSGltYW5zaHUgb24gMjAvMDkvMjEuCi8vCgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKdm9pZCBwcmludFBhc2NhbFRyaWFuZ2xlIChpbnQgbikgewogICAgaW50IHBhc2NhbFtuKzFdW24rMV07CiAgICAKICAgIC8vQmFzZSBjYXNlCiAgICBwYXNjYWxbMV1bMV0gPSAxOwogICAgCiAgICBmb3IgKGludCBpPTA7IGk8PW47IGkrKykgewogICAgICAgIGZvciAoaW50IGo9MDsgajw9bjsgaisrKSB7CiAgICAgICAgICAgIHBhc2NhbFtpXVtqXSA9IDA7CiAgICAgICAgfQogICAgfQogICAgCiAgICBmb3IgKGludCBpPTE7IGk8PW47IGkrKykgewogICAgICAgIGZvciAoaW50IGo9MTsgajw9aTsgaisrKSB7CiAgICAgICAgICAgIC8vIGZpcnN0IGFuZCBsYXN0IGJpbm9taWFsIGNvZWZmaWNpZW50cyBhcmUKICAgICAgICAgICAgLy8gYWx3YXlzIDEKICAgICAgICAgICAgaWYgKGkgPT0gMSB8fCBqID09IGkpIHsKICAgICAgICAgICAgICAgIHBhc2NhbFtpXVtqXSA9IDE7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwYXNjYWxbaV1bal0gPSBwYXNjYWxbaS0xXVtqLTFdICsgcGFzY2FsW2ktMV1bal07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAKICAgIGZvciAoaW50IGk9MTsgaTw9bjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaj0xOyBqPD1pOyBqKyspIHsKICAgICAgICAgICAgY291dDw8cGFzY2FsW2ldW2pdPDwiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQ8PGVuZGw7CiAgICB9CiAgICAKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgbiA9IDc7CiAgICBwcmludFBhc2NhbFRyaWFuZ2xlIChuKTsKICAgIAogICAgcmV0dXJuIDA7Cn0K