0
0
CppProgramBeginner · 2 min read

C++ Program to Print Pascal Triangle with Output and Explanation

You can print Pascal's triangle in C++ by using nested for loops and calculating each value with the formula value = value * (row - col) / (col + 1) inside the loops to generate each row.
📋

Examples

Input5
Output1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Input1
Output1
Input0
Output
🧠

How to Think About It

To print Pascal's triangle, think of each row as a list of numbers where each number is the sum of the two numbers directly above it from the previous row. Start with 1 at the top. Use loops to build each row by calculating values based on the previous ones or by using a formula that calculates each number directly.
📐

Algorithm

1
Get the number of rows to print from the user.
2
For each row from 0 to number of rows - 1, do:
3
Start with the first value as 1.
4
For each position in the row, calculate the next value using the formula: value = value * (row - col) / (col + 1).
5
Print each value followed by a space.
6
Move to the next line after printing each row.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int rows;
    cin >> rows;
    for (int i = 0; i < rows; i++) {
        long long val = 1;
        for (int j = 0; j <= i; j++) {
            cout << val << " ";
            val = val * (i - j) / (j + 1);
        }
        cout << "\n";
    }
    return 0;
}
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
🔍

Dry Run

Let's trace printing 3 rows of Pascal's triangle through the code

1

Start first row (i=0)

val = 1; print 1; no more elements in this row

2

Start second row (i=1)

val = 1; print 1; calculate next val = 1 * (1-0)/(0+1) = 1; print 1

3

Start third row (i=2)

val = 1; print 1; val = 1 * (2-0)/(0+1) = 2; print 2; val = 2 * (2-1)/(1+1) = 1; print 1

Row (i)Col (j)val before printval after update
001N/A
1011
111N/A
2012
2121
221N/A
💡

Why This Works

Step 1: Initialize first value

Each row starts with val = 1 because the first number in Pascal's triangle is always 1.

Step 2: Calculate next values

Use the formula val = val * (row - col) / (col + 1) to find the next number in the row based on the previous number.

Step 3: Print values row by row

Print each calculated value followed by a space, then move to the next line after finishing a row.

🔄

Alternative Approaches

Using 2D array to store and print
cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int pascal[50][50] = {0};
    for (int i = 0; i < n; i++) {
        pascal[i][0] = 1;
        for (int j = 1; j <= i; j++) {
            pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            cout << pascal[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}
This method uses extra memory to store all values but is easier to understand by summing previous row values.
Recursive function to calculate binomial coefficients
cpp
#include <iostream>
using namespace std;

int binomial(int n, int k) {
    if (k == 0 || k == n) return 1;
    return binomial(n-1, k-1) + binomial(n-1, k);
}

int main() {
    int rows;
    cin >> rows;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j <= i; j++) {
            cout << binomial(i, j) << " ";
        }
        cout << "\n";
    }
    return 0;
}
This recursive approach is simple but inefficient for large rows due to repeated calculations.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.

Space Complexity

Only a few variables are used to calculate values on the fly, so space complexity is O(1).

Which Approach is Fastest?

The formula-based approach is fastest and uses constant space, while the 2D array uses more memory and recursion is slow due to repeated calls.

ApproachTimeSpaceBest For
Formula-based calculationO(n^2)O(1)Efficient and memory-friendly
2D array storageO(n^2)O(n^2)Easy to understand, uses more memory
Recursive calculationExponentialO(n)Simple code but slow for large n
💡
Use the formula val = val * (row - col) / (col + 1) to efficiently calculate Pascal's triangle values without extra memory.
⚠️
Beginners often forget to use a long long type for val, causing overflow for larger rows.