C++ Program For Pascal's Triangle

Last Updated : 24 Jun, 2026

Pascal's triangle is a triangular arrangement of binomial coefficients where every row begins and ends with 1, and every other element is the sum of the two elements directly above it.

  • Each row represents the coefficients of a binomial expansion.
  • It can be generated using several approaches with different time and space complexities.

Example 

1  
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Methods to Generate Pascal's Triangle

This article discusses three approaches to generate Pascal's Triangle, each with different time and space complexities.

Method 1: Using Binomial Coefficients

In this approach, every element is computed independently using the binomial coefficient formula.

  • Each row contains row + 1 elements.
  • The iᵗʰ element in row row is C(row, i).
  • Every coefficient is calculated separately.

Formula

C(n, k) = n! / (k! × (n − k)!)

Algorithm

  • Iterate through each row.
  • For every position in the row, calculate the corresponding binomial coefficient.
  • Print the computed value.
C++
#include <iostream>
using namespace std;

// See https://www.geeksforgeeks.org/dsa/space-and-time-efficient-binomial-coefficient/ 
// for details of this function
long long binomialCoeff(int n, int k);

// Function to print first
// n lines of Pascal's 
// Triangle
void printPascal(int n)
{
    // Iterate through every line and
    // print entries in it
    for (int line = 0; line < n; line++)
    {
        // Every line has number of 
        // integers equal to line 
        // number
        for (int i = 0; i <= line; i++)
            cout <<" "<< binomialCoeff(line, i);
        cout <<"\n";
    }
}

// See https://www.geeksforgeeks.org/dsa/space-and-time-efficient-binomial-coefficient/
// for details of this function
long long binomialCoeff(int n, int k)
{
    long long res = 1;
    if (k > n - k)
    k = n - k;
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    
    return res;
}

// Driver program 
int main()
{
    int n = 7;
    printPascal(n);
    return 0;
}

Output

1 
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

Complexity Analysis

  • Time Complexity: O(n³)
  • Auxiliary Space: O(1)

Method 2: Using Dynamic Programming 

Instead of calculating every coefficient independently, use the property that every element is the sum of the two elements directly above it.

  • The first and last element of every row is always 1.
  • Remaining elements are computed using previously generated rows.
  • Stores the triangle in a 2D array.

Algorithm

  • Create a 2D array.
  • Initialize the first and last element of every row as 1.
  • Compute the remaining elements using previous rows.
  • Print the generated triangle.
C++
#include <bits/stdc++.h>
using namespace std;

void printPascal(int n)
{
    
    // An auxiliary array to store 
    // generated pascal triangle values
    long long arr[n][n]; 

    // Iterate through every line and 
    // print integer(s) in it
    for (int line = 0; line < n; line++)
    {
        // Every line has number of integers 
        // equal to line number
        for (int i = 0; i <= line; i++)
        {
        // First and last values in every row are 1
        if (line == i || i == 0)
            arr[line][i] = 1;
        // Other values are sum of values just 
        // above and left of above
        else
            arr[line][i] = arr[line - 1][i - 1] + 
                            arr[line - 1][i];
        cout << arr[line][i] << " ";
        }
        cout << "\n";
    }
}

// Driver code
int main()
{
    int n = 5;
    printPascal(n);
    return 0;
}

Output

1 
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Complexity Analysis

  • Time Complexity: O(n²)
  • Auxiliary Space: O(n²)

Space Optimization

The previous method stores the entire Pascal's Triangle even though only the previous row is required to generate the next row.

  • The extra space can be reduced to O(n) by storing only one row.
  • It can be optimized even further by computing each coefficient directly without storing previous rows, as shown in the next method.


Method 3: Using Previous Binomial Coefficient

This is the most efficient approach. Instead of recomputing every coefficient, each value is calculated from the previous coefficient in constant time.

  • Every row starts with 1.
  • Each subsequent coefficient is derived from the previous one.
  • No extra array is required.

Formula

C(line, i) = C(line, i − 1) × (line − i + 1) / i

Algorithm

  • Start every row with coefficient 1.
  • Print the current coefficient.
  • Compute the next coefficient using the recurrence relation.
  • Repeat until the row is complete.
C++
#include <bits/stdc++.h>

using namespace std;
void printPascal(int n)
{
    
for (int line = 1; line <= n; line++)
{
    long long C = 1; // used to represent C(line, i)
    for (int i = 1; i <= line; i++) 
    {
        
        // The first value in a line is always 1
        cout<< C<<" "; 
        C = C * (line - i) / i; 
    }
    cout<<"\n";
}
}

// Driver code
int main()
{
    int n = 5;
    printPascal(n);
    return 0;
}

Output

1 
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Complexity Analysis

  • Time Complexity: O(n²)
  • Auxiliary Space: O(1)

Integer Overflow

Although the optimized approach is the most space-efficient, Pascal's Triangle grows very quickly.

  • Using long long increases the range compared to int.
  • However, even long long overflows for sufficiently large values of n.
  • For very large rows, arbitrary-precision arithmetic is required.
Comment