codescracker
c++

C++ Program to Print Pascal Triangle



« Previous Program Next Program »


In this article, you will learn and get code to print Pascal's triangle using C++ program. Here are the list of programs on printing Pascal's triangle:

Before creating these programs, let's understand about, how a Pascal's triangle gets created.

What is Pascal's Triangle ?

The figure given below shows everything about the Pascal's triangle:

pascal triangle c++

In this way, Pascal's triangle can be formed or created. To get its in-depth detail like:

or whatever you want to learn about it, refer to Pascal's Triangle to get every required things about the topic.

Print Pascal's Triangle

This program prints Pascal's triangle of 5 rows or lines. This program doesn't allows user to define the size of Pascal's triangle. Later on, the same program is created that receives input from user to define the size of Pascal's triangle.

// C++ Program to Print Pascal's Triangle
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int row, col, i=1, j=0, arr[5], arrTemp[5];
    arr[0] = 1;
    arr[1] = 1;
    for(row=0; row<5; row++)
    {
        for(col=4; col>row; col--)
            cout<<" ";
        for(col=0; col<=row; col++)
        {
            if(row==0)
                cout<<"1";
            else
            {
                if(col==0 || col==row)
                    cout<<"1 ";
                else
                {
                    arrTemp[i] = arr[j]+arr[j+1];
                    cout<<arrTemp[i]<<" ";
                    i++;
                    j++;
                }
            }
        }
        cout<<endl;
        arrTemp[i] = 1;
        if(row>1)
        {
            j=0;
            arr[j]=1;
            for(j=1, i=1; j<=row; j++, i++)
                arr[j] = arrTemp[i];
            i=1;
            j=0;
        }
    }
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample output:

C++ program print pascal triangle

Print Pascal's Triangle using Function and Formula

Now let's create another program that does the same job as of previous program. That is, this program also prints Pascal's triangle. The only difference is, here we've used a user-defined function, fact(), that is used to return factorial of a number passed as its argument, and a formula to find column's value of every row:

// C++ Pascal's Triangle using Function and Formula
// ----codescracker.com----

#include<iostream>
using namespace std;
long int fact(int);
int main()
{
    int i, c;
    for(i=0; i<5; i++)
    {
        for(c=4; c>i; c--)
            cout<<" ";
        for(c=0; c<=i; c++)
            cout<<fact(i)/(fact(c)*fact(i-c))<<" ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}
long int fact(int n)
{
    int i, f=1;
    for(i=1; i<=n; i++)
        f = f*i;
    return f;
}

This program produces the same output as of previous program. The formula used in above program to find the column's value of every row is:

value = (row!)/((column!)*(row-columns)!)

Note - The ! indicates factorial. To learn about finding the factorial of a number in C++, then refer to the separate tutorial on it.

Note - Row and column both starts from 0.

For example, to find the value at 2nd column of 4th row. Here is the step-by-step calculation of the value using above formula:

value = (row!)/((column!)*(row-columns)!)
      = (4!)/((2!)*(4-2)!)
      = (24)/(2*(2!))
      = 24/(2*2)
      = 24/4
      = 6

So 6 is the value that has to be present at fourth row and second column.

Print Pascal's Triangle upto n Rows

This is the last program on printing of Pascal's Triangle. This program allows user to define the size of Pascal's triangle. That is, upto how many rows, user wants to expand Pascal's triangle.

// C++ Print Pascal's Triangle upto n Rows
// ----codescracker.com----

#include<iostream>
using namespace std;
long int fact(int);
int main()
{
    int i, c, rowSize;
    cout<<"Enter the Number of Rows: ";
    cin>>rowSize;
    for(i=0; i<rowSize; i++)
    {
        for(c=(rowSize-1); c>i; c--)
            cout<<" ";
        for(c=0; c<=i; c++)
            cout<<fact(i)/(fact(c)*fact(i-c))<<" ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}
long int fact(int n)
{
    int i, factRes=1;
    for(i=1; i<=n; i++)
        factRes = factRes*i;
    return factRes;
}

Here is the initial output of this program's sample run:

print pascal triangle upto n rows c++

Now supply the input as number of rows to print Pascal's triangle. Here is the sample output with user input 8 as rows size:

c++ print pascal triangle

Same Program in Other Languages


« Previous Program Next Program »