Signup/Sign In

C++ Program To Print A Pascal Triangle

Here our task is to print the required pattern without actually writing it manually. We will see how to do this for Pascal's triangle pattern. The simplest case will be to make the pattern using * only.

Following is the program to print Pascal's triangle using *.

#include <iostream>
using namespace std;

int main()
{
    int rows, coef = 1;

    cout << "Enter number of rows: ";
    cin >> rows;

    for(int i = 0; i < rows; i++)
    {
        for(int space = 1; space <= rows-i; space++)
            cout <<"  ";

        for(int j = 0; j <= i; j++)
        {
            if (j == 0 || i == 0)
                coef = 1;
            else
                coef = coef*(i-j+1)/j;

            cout << coef << "   ";
        }
        cout << endl;
    }

    return 0;
}


Enter number of rows: 4
1
1 1
1 2 1
1 3 3 1