Write a Program to Print the Pascal Triangle

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

C++// C++ program to print // Pascal’s Triangle #include <iostream> using namespace std; void printPascal(int n) { int arr[n][n]; 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; else arr[line][i] = arr[line - 1][i - 1] + arr[line - 1][i]; cout << arr[line][i] << " "; } cout << "\n"; } } int main() { int n = 6; 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 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *