Pascal's Triangle in C++
In this lesson: Print Pascal's triangle in C++ using nested loops and nCr — code, dry run and output. In Hindi & English.
What is Pascal's Triangle
Pascal's Triangle is a famous number pattern shaped like a triangle. Every row starts and ends with 1, and each number inside is the sum of the two numbers directly above it. It is a classic programming exercise because it needs nested loops and a simple formula.
The pattern and rule
Here are the first five rows:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Look at row 4 (1 4 6 4 1): the 6 in the middle is 3 + 3 from the row above. Every inside number follows this "sum of the two above" rule, while the ends stay 1.
The C++ program
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for (int i = 0; i < rows; i++) {
int val = 1; // first number of each row is 1
for (int j = 0; j <= i; j++) {
cout << val << " ";
val = val * (i - j) / (j + 1); // next number
}
cout << endl; // new line after each row
}
return 0;
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
The outer loop i handles each row; the inner loop j prints the numbers in that row. Each row starts with val = 1, and the formula computes the next number as we go.
How the formula works
Instead of storing the previous row, we build each number from the one before it using:
val = val * (i - j) / (j + 1);
This is the binomial coefficient shortcut. Starting from 1, it produces the next value in the row directly. For row 4: start 1, then 1×4/1 = 4, then 4×3/2 = 6, then 6×2/3 = 4, then 4×1/4 = 1 — giving 1 4 6 4 1.
Dry run
| Row (i) | Values (from the formula) |
|---|---|
| 0 | 1 |
| 1 | 1 1 |
| 2 | 1 2 1 |
| 3 | 1 3 3 1 |
| 4 | 1 4 6 4 1 |
Each row starts at 1. The next number is built from the previous one with the formula val * (i - j) / (j + 1) (the binomial coefficient). Each number is also the sum of the two above it — that is the heart of Pascal's Triangle.
Common mistakes
- Resetting
valwrongly. Setval = 1at the start of each row, not once before all rows. - Integer division order. Write
val * (i - j) / (j + 1)(multiply first) so integer division stays exact. - Wrong inner loop limit. Row
ihasi + 1numbers, so loopjfrom 0 toiinclusive. - Forgetting the newline. Print
endlafter each row, or all numbers run together.
Summary
- Pascal's Triangle: edges are 1, each inside number is the sum of the two above.
- Use nested loops — outer for rows, inner for the numbers in a row.
- Start each row at
val = 1and use the binomial formula for the next number. - Row
ihasi + 1numbers. - Multiply before dividing to keep integer maths exact.
Next, practise more shapes in nested loop patterns, or revise loops in C++ loops.
Frequently Asked Questions
What is Pascal's Triangle?
How is each number in Pascal's Triangle calculated?
val = val * (i - j) / (j + 1), which builds each row from the previous value without needing the row above stored.Why does every row begin and end with 1?
How many numbers are in each row?
i (counting from 0) has i + 1 numbers. So row 0 has 1 number, row 1 has 2, row 2 has 3, and so on — the triangle widens by one each row.