Sunday 7 August 2011

C++ program to generate pascal's triangle without using array

Hai friends,
Here i give   c++ program to generate Pascal's  triangle. Many people use 2D array to generate Pascal's triangle.The use of 2D array may be complex. Use the following program.Try this simple code.

Pascal's triangle for the limit 6

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



C++ Program to generate Pascal's Triangle


#include<iostream>
using namespace std;
#include<conio.h>
int fact(int);
int main()
{
     int t,r,p=1,f,k,n,c,i,j;
     cout<<"Enter the limit \n";
     cin>>n;
     for(i=1;i<=n;i++,p++)
     {
                     k=0;
     for(j=0;j<=i;j++,k++)
     {
              t=fact(p);
     r=fact(p-k);
     f=fact(k);
      c=t/(f*r);
     cout<<c<<'\t';
     }
     cout<<'\n';
     }
     getch();
     }
     int fact(int x)
     {
         int z,f=1;
         if(x==0)
         return(1);
         else
         for(z=1;z<=x;z++)
         f=f*z;
         return(f);
         } 



Sample Output:
Enter the limit
5

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

1 comments:

Pascal triangle is the basic lab c program used in many institutions. Thank you for sharing this program.
regards:
srinath reddy.
admin of Programming Tutorials for Beginners

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More