Pascal’s triangle is a triangular array of numbers. Edges are always 1. Every inner value is the sum of the two numbers above it. Row n (0-based) holds C(n, 0) … C(n, n).
Remember
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Rule: value = left_parent + right_parent
(Edges have only one parent → stay 1)
Rows
Output
1
1
3
1 / 1 1 / 1 2 1
5
Up through 1 4 6 4 1
Approach
How to Generate Pascal’s Triangle in C++
Four clear steps — then pick an additive row build or a multiplicative coefficient fill.
Decide how many rows r to print (start rows at 0 or 1 — stay consistent).
Start with row [1].
For each next row, edges are 1; inner cells are the sum of adjacent values from the previous row.
Or fill one row at a time with C *= (n - k) / (k + 1) (multiplicative update).
Method
Idea
Time
Best for
Additive
Sum adjacent values from the previous row
O(r²)
Teaching the geometric rule
Multiplicative
Build each row with binomial updates
O(r²)
No previous-row storage
Pseudocode
Pseudocode
function PrintPascal(rows):
prev = [1]
print prev
for i from 1 to rows - 1:
curr = [1]
for j from 1 to length(prev) - 1:
curr.append(prev[j - 1] + prev[j])
curr.append(1)
print curr
prev = curr
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Edges
Always 1
Inner cell
Sum of the two parents above
Binomial link
Row n, spot k = C(n, k)
Symmetric
C(n, k) = C(n, n - k)
Fast update
C = C * (n - k) / (k + 1)
Classic check
Row 4 → 1 4 6 4 1
Try it
Live Preview
Enter how many rows to print and watch the triangle grow.
Positive integers from 1 to 12. Tap a chip or type a value.
Result
rows = 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Trace
Worked Walkthrough — r = 5
Build each new row from the previous one by adding neighbors.
Start with the additive method so you can explain every cell. Then use the multiplicative update when you want each row without storing the previous one.
Example 1 — Additive Rows
Keep the previous row and build the next by summing adjacent values.
C++
#include <iostream>
#include <vector>
using namespace std;
void printPascal(int rows)
{
if (rows <= 0)
{
cout << "Enter a positive number of rows." << endl;
return;
}
vector<int> prev = { 1 };
cout << prev[0] << endl;
for (int i = 1; i < rows; i++)
{
vector<int> curr = { 1 };
for (size_t j = 1; j < prev.size(); j++)
{
curr.push_back(prev[j - 1] + prev[j]);
}
curr.push_back(1);
for (size_t j = 0; j < curr.size(); j++)
{
if (j > 0)
{
cout << " ";
}
cout << curr[j];
}
cout << endl;
prev = curr;
}
}
int main()
{
printPascal(5);
return 0;
}
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
How It Works
Reject non-positive row counts before looping.
Start with prev = {1} — the top of the triangle.
Each new row begins and ends with 1.
Inner values are prev[j - 1] + prev[j] — the classic “two parents” rule.
Replace prev with curr and repeat until rows lines are printed.
Example 2 — Multiplicative Row
Fill each row with the binomial update — no previous row required.
C++
#include <iostream>
using namespace std;
void printPascal(int rows)
{
if (rows <= 0)
{
cout << "Enter a positive number of rows." << endl;
return;
}
for (int n = 0; n < rows; n++)
{
long long c = 1;
for (int k = 0; k <= n; k++)
{
cout << c;
if (k < n)
{
cout << " ";
}
c = c * (n - k) / (k + 1);
}
cout << endl;
}
}
int main()
{
printPascal(5);
return 0;
}
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
How It Works
Outer loop chooses row index n from 0 to rows - 1.
Start each row with c = 1, which is C(n, 0).
Print c, then update c = c * (n - k) / (k + 1) to get C(n, k + 1).
Integer division is exact here because binomial ratios stay whole.
Same triangle as Example 1, with O(1) extra space beyond the printed line.
Edge Cases & Pitfalls
Check these before calling the solution done.
r ≤ 0
Invalid rows
Ask for a positive row count before printing.
Indexing
0-based vs 1-based
Be clear whether row 1 is [1] or [1 1]. Stay consistent in code and tests.
Overflow
Large rows
Binomial values grow fast. Prefer long long for bigger triangles.
Division order
Multiply before divide
Use c * (n - k) / (k + 1), not divide first, so intermediate results stay exact.
Analysis
Time and Space Complexity
Program
Time
Extra space
Additive rows
O(r²)
O(r) for previous row
Multiplicative row
O(r²)
O(1) beyond output
Both print about 1 + 2 + … + r = r(r+1)/2 numbers, so time is quadratic in the row count.
Remember
Key Takeaways
Rule: edges are 1; inner = sum of the two parents above.
Binomial: row n is C(n, 0) … C(n, n).
Update:c = c * (n - k) / (k + 1) fills a row fast.
Test set: 5 rows end with 1 4 6 4 1.
One line: each inner entry is the sum of the two numbers above it — which is also C(n, k).
Frequently Asked Questions
A triangle of numbers where each edge value is 1, and every inner value is the sum of the two numbers directly above it. Row n holds the binomial coefficients C(n, 0) … C(n, n).
Build each row from the previous by adding adjacent pairs (additive method), or fill a row with the multiplicative update C *= (n - k) / (k + 1).
C(n, 0) and C(n, n) are always 1. Geometrically, nothing sits outside the triangle to add into those corners.
Yes. Each binomial update lands on an exact integer, so C++ integer division is correct for that step.
Printing r rows needs O(r²) time. Additive uses O(r) extra space for the previous row; multiplicative can use O(1) extra beyond the output.