Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 81

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 edges are always 1, and every inside number = the two numbers above it added together.

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;
}
Output:
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

Dry Run — Pascal's Triangle (how each row is built)
Row (i)Values (from the formula)
01
11 1
21 2 1
31 3 3 1
41 4 6 4 1
💡 Understand it this way

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 val wrongly. Set val = 1 at 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 i has i + 1 numbers, so loop j from 0 to i inclusive.
  • Forgetting the newline. Print endl after 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 = 1 and use the binomial formula for the next number.
  • Row i has i + 1 numbers.
  • 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?
Pascal's Triangle is a triangular arrangement of numbers where every row starts and ends with 1, and every inside number is the sum of the two numbers directly above it. It appears often in maths, especially in binomial expansions.
How is each number in Pascal's Triangle calculated?
Each number is the sum of the two numbers above it. In code, a common shortcut is the formula 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?
The first and last positions of each row have only one number above them (a 1) rather than two to add, so they stay 1. This is why the outer edges of the whole triangle are all 1s.
How many numbers are in each row?
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.
What is Pascal's Triangle used for?
Besides being a classic pattern exercise, it gives the coefficients of a binomial expansion (like (a+b)^n), and it is used in probability and combinations. Learning to print it also builds strong nested-loop skills.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Course complete! Continue withJava Your progress: 81 / 81 (100%) · Recommended next course Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free