C++ Program to Print Pascal's Triangle

Write a C++ program to print Pascal's triangle. Print Pascal's triangle in c++. This C++ program accepts the number of rows from user and prints the pascal's triangle based on rows.

C++ program to print Pascal's triangle

/* Print Pascal's triangle in c++ */

#include<iostream>
using namespace std;
 
int main()
{
    int rows;

    cout << "Enter the number of rows : ";
    cin >> rows;
 
    for (int i = 0; i < rows; i++)
    {
        int val = 1;

        for (int j = 1; j < (rows - i); j++)
        {
            cout << "   ";
        }

        for (int k = 0; k <= i; k++)
        {
            cout << "      " << val;
            val = val * (i - k) / (k + 1);
        }

        count << endl << endl;
    }

    cout << endl;

    return 0;

}

Output:

Enter number of rows: 6
           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1