Skip to content
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

pascal's triangle code #65

Open
wants to merge 1 commit into
base: main
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
38 changes: 38 additions & 0 deletions pascals_triangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>

using namespace std;

void generate(int numRows)
{
vector<vector<int>> ans(numRows);

for (int i = 0; i < numRows; i++)
{
ans[i].resize(i + 1); // ith row has i+1 elements

ans[i][0] = ans[i][i] = 1; // for each row 0th and ith is 1

// loop to calculate sum of remaining elements from previous row
for (int j = 1; j < i; j++) // this will start from row 3 for i=2
ans[i][j] = ans[i - 1][j] + ans[i - 1][j - 1];
}
cout << "Pascals Triangle " << endl;
for (int i = 0; i < ans.size(); ++i)
{
for (int j = 0; j < ans[i].size(); j++)
{
cout << ans[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int n;
cout << "enter number of rows " << endl;
cin >> n;

generate(n);
return 0;
}