C++ Program to Print Pascal Triangle using function

By | 25.12.2016

Print Pascal Triangle using function


Write a C++ Program to Print Pascal Triangle using function. Here’s simple Program to Print Pascal Triangle using function in C++ Programming Language.


Pascal’s Triangle


Pascal’s triangle is a triangular array of the binomial coefficients. Write a function that takes an integer value n as input and prints first n lines of the Pascal’s triangle. Following are the first 6 rows of Pascal’s Triangle.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1


Here is source code of the C++ Program to Print Pascal Triangle using function. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/* C++ Program to Print Pascal Triangle using function  */

#include<iostream>

using namespace std;

//function to calculate factorial
long fact(int x)
{
    int i;
    long f=1;

    for(i=1;i<=x;++i)
    {
        f=f*i;
    }

    return f;
}

int main()
{
    int i,j,k,n;

    cout<<"How many lines u want to Print :: ";
    cin>>n;

    for(i=0;i<n;++i)
    {
        //loop to print spaces at starting of each row
        for(j=1;j<=(n-i-1);++j)
        {
            cout<<" ";
        }

        //loop to calculate each value in a row and print it
        for(k=0;k<=i;++k)
        {
             cout<<fact(i)/(fact(i-k)*fact(k))<<" ";
        }

        cout<<"\n";    //print new line after each row
    }

    return 0;
}

OUTPUT : :


/* C++ Program to Print Pascal Triangle using function */

How many lines u want to Print :: 10

         1
        1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1
    1 5 10 10 5 1
   1 6 15 20 15 6 1
  1 7 21 35 35 21 7 1
 1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Process returned 0

Above is the source code for C++ Program to Print Pascal Triangle using function which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.2 6 votes
Article Rating
Category: Basic Programs C++ Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dave

#include <iostream> using namespace std; void Pascal(int row){ int col=row; int Pt[row][col], f, spc=col; for(int i=0; i<row; i++){ for(int e=1; e<col; e++){ Pt[i][e]=0; } } for(int i=0; i<row; i++){ Pt[i][0]=1; } for(int i=1; i<row; i++){ for(int e=1; e<col; e++){ Pt[i][e] = Pt[i–1][e] + Pt[i–1][e–1]; } } for(int i=0; i<row; i++){ f=spc; for(;f>0;f—){ cout << “ “; } for(int e=0; e<col; e++){ if(Pt[i][e]!=0){ cout << “ “ << Pt[i][e]; } } cout << endl; spc= spc–1; } } int main(){ int row; cout << “Enter the number of rows you want to print of Pascal Triangle: “; cin >> row; Pascal(row);… Read more »

mary

how does the function fact works