Program to Print Pascal Triangle

This is a C++ Program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user.

So, the code would be like this:


#include <iostream>
using namespace std;
long factorial(int n)
{
    long result = 1;
    for(int c = 1; c <= n; c++)
    {
        result = result * c;
    }
    return result;
}

int main()
{
    int i, n, c;
    cout << "Enter the number of rows you wish to see in pascal triangle : ";
    cin >> n;
    for (i = 0; i < n; i++ ) //rows
    {
        for (c = 0; c <= (n - i - 2); c++)
        {
            cout<<" ";
        }
        for(c = 0; c <= i; c++ )
        {
            cout << factorial(i) / (factorial(c) * factorial(i-c)) << " ";
        }
        cout<<endl;
    }
    return 0;
}
PS: I am using Code::Blocks 16.01
Now, lemme give you my screenshot, so you can see like what I'm typing this code.


Output of this program:


Thank you! :)

Comments

Popular posts from this blog