Write a C++ program to display Pascal’s triangle using the Constructor Overloading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
#include<iostream> using namespace std; class T4Tutorials { protected: int r,s,i,j; int c; int c1; int c2; public: T4Tutorials(int T4Tutorials1) { for(i=0;i<T4Tutorials1;i++) { for(s=1;s<=T4Tutorials1-i;s++) { cout<<" "; } for(j=0;j<=i;j++) { if(j==0||i==0) { c=1; } else { c=c*(i-j+1)/j; } cout<<c<<" "; } cout<<endl; } } T4Tutorials(int T4Tutorials2,int T4Tutorials3) { for(i=0;i<T4Tutorials2;i++) { for(s=1;s<=T4Tutorials2-i;s++) { cout<<" "; } for(j=0;j<=i;j++) { if(j==0||i==0) { c1=1; } else { c1=c1*(i-j+1)/j; } cout<<c1<<" "; } cout<<endl; } cout<<"\n\n\n\2nd T4Tutorials\n"; for(i=0;i<T4Tutorials3;i++) { for(s=1;s<=T4Tutorials3-i;s++) { cout<<" "; } for(j=0;j<=i;j++) { if(j==0||i==0) { c2=1; } else { c2=c2*(i-j+1)/j; } cout<<c2<<" "; } cout<<endl; } } }; int main() { int option; cout<<"Enter 1 of Single parameter constructor \n"; cout<<"\nEnter 2 For Multiple Paramter constucor "<<endl; cin>>option; if(option ==1) { cout<<"This will show single parameter constructor "<<endl; int T4Tutorials1; cout<<"Enter number of rows to show T4Tutorials : "; cin>>T4Tutorials1; T4Tutorials a(T4Tutorials1); } if(option==2) { cout<<"This will show multiple parameter constructor "<<endl; int T4Tutorials2,T4Tutorials3; cout<<"Enter number of rows for 1st T4Tutorials : "; cin>>T4Tutorials2; cout<<"\nEnter number of rows for 2nd T4Tutorials : "; cin>>T4Tutorials3; T4Tutorials a(T4Tutorials2, T4Tutorials3); } } |
Output