Print Pascal's Triangle
C++
Hard
3 views
Problem Description
Print Pascal's triangle with numbers.
Real Life: Important mathematical triangle in combinatorics.
Step-by-Step Logic:
1. Each row starts and ends with 1
2. Middle elements = sum of two above elements
3. Calculate combination formula
4. Or use previous row values
Official Solution
void pattern_q12_pascals_triangle() {
int n = 5;
for(int i = 0; i < n; i++) {
// Print spaces for alignment
for(int j = 0; j < n - i - 1; j++) {
cout << " ";
}
int value = 1;
for(int j = 0; j <= i; j++) {
cout << value << " ";
value = value * (i - j) / (j + 1);
}
cout << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!