C++ Program to Print Pascal Triangle with Output and Explanation
for loops and calculating each value with the formula value = value * (row - col) / (col + 1) inside the loops to generate each row.Examples
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace printing 3 rows of Pascal's triangle through the code
Start first row (i=0)
val = 1; print 1; no more elements in this row
Start second row (i=1)
val = 1; print 1; calculate next val = 1 * (1-0)/(0+1) = 1; print 1
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 print | val after update |
|---|---|---|---|
| 0 | 0 | 1 | N/A |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | N/A |
| 2 | 0 | 1 | 2 |
| 2 | 1 | 2 | 1 |
| 2 | 2 | 1 | N/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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Formula-based calculation | O(n^2) | O(1) | Efficient and memory-friendly |
| 2D array storage | O(n^2) | O(n^2) | Easy to understand, uses more memory |
| Recursive calculation | Exponential | O(n) | Simple code but slow for large n |
val = val * (row - col) / (col + 1) to efficiently calculate Pascal's triangle values without extra memory.val, causing overflow for larger rows.