C++

Pascal’s Triangle in C++

In C++ different geometric shapes can be printed using different sets of loops that include pyramids, rectangles, squares, and so on. A specific addition to the triangles family in C++ is Pascal’s Triangle which uses a specific algorithm to print the elements in a triangular shape.

Pascal’s Triangle in C++

Pascal’s Triangle in C++ is an array of binomial coefficients arranged in a triangular manner. The number of elements in each row is equal to the number of rows, and the first and last elements of each row are set to 1. Every entry in the line is the binomial coefficient and the value of the number is chosen by applying additive property in such a way that every element in the triangle is obtained by adding the above two elements and left of above too. The Formula of Binomial Coefficient for Pascal’s Triangle

C(line, n)   = line! / ( (line-n)! * n )

The simple method to implement Pascal’s Triangle is to run loops and implement the binomial coefficient method in each loop.

Pascal’s Triangle Using a Nested Loop

This is a source code that is used to print Pascal’s Triangle in C++:

#include<iostream>

using namespace std;



int main()

{

    int rows;

    cout << "Enter the number of rows for Pascal's Triangle': ";

    cin >> rows;

    cout << endl;



    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);
   
        }

        cout << endl << endl;

    }

    cout << endl;

    return 0;

}

The user is asked to enter the number of rows for Pascal’s Triangle. The for loop is used to iterate through each row starting from the 0th row and keeps on iterating until the maximum number of rows set by the user is reached. A space of three spaces is added between each number to give the triangle a desired shape. The for loop is used to keep the number of rows and elements the same in a line. Then the binomial coefficient formula is applied to calculate the values of the elements in the triangle.

The user enters the number of rows of triangles to be 10. Pascal’s Triangle according to the set parameters is printed on the console window.

Right Angled Pascal’s Triangle

This is an example program written to print Right Angled Pascal’s Triangle.

#include <bits/stdc++.h>

using namespace std;



void printPascal(int n)

{



    int arr[n][n];



    for (int line = 0; line < n; line++)

    {

        for (int i = 0; i <= line; i++)

        {



        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 ;

    cout << "Enter the number of rows for Pascal's Triangle': ";

    cin >>n ;

    printPascal(n);

    return 0;

}

In this source code, <bits/stdc++.h> header file is declared that contains all the standard libraries to print a Pascal’s Triangle. An auxiliary array is declared that is capable of storing elements for the triangle of a defined number. The iterator starts from the 0th line and keeps on iterating till the maximum set limit. The number of elements in a line is set to be equal to the line number, and the first and last elements of each are fixed to be 1. Other values in the triangle are the sum of the above-present numbers and left above too. In the main section, it asks the user to enter several lines to be printed in the triangle.

The user enters several 6 for the lines in Pascal’s Triangle, and the algorithm is executed to print the right-angled Pascal’s triangle.

Conclusion

Pascal’s Triangle in C++ is an array of binomial coefficients arranged in a triangular manner. An auxiliary array is declared that is capable of storing elements for the triangle of a defined number. The number of elements in each row is equal to the number of rows, and the first and last elements of each row are set to 1. Every entry in the line is the binomial coefficient and the value of the number is chosen by applying additive property in such a way that every element in the triangle is obtained by adding the above two elements and left of above too.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.