Pascal Triangle in C++ using Recursive Function

This code is the simple demonstration of Pascal triangle in which you can tell the row and column count and it will return you the value at that specific row column count.it is the very interesting number pattern found in mathematics.

Source Code

#include <iostream>

using namespace std;

int compute_pascal(int row, int position);
int main()
{
        int row, position;
        cout<<"Please input a row Number ";
        cin>>row;
        cout<<"Please input  position along the row # "<<row<<" : ";
        cin>>position;
        if(row<position)
        {
   cout<<"\nRow : "<<row;
   cout<<"\nPosition: "<<position;
     
            cout<<"\nInvalid entry.  Position must be less than or equal to row.";
            return 0;
        }else{
   cout<<"\nValue at row "<<row<<" and position " <<position<<" is "<<compute_pascal(row, position); 
  }
        
}
int compute_pascal(int row, int position)
{
 if(position == 1)
 {
  return 1;
 }
 else if(position == row)
 {
  return 1;
 }
 else
 {
  return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);
 }
}

Output of the Program

pascal triangle c++ program

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment