Write a program in C++ to display the pattern like a pyramid using an asterisk * and each row contains an odd number of asterisks by using the class objects.
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 |
# include<iostream> using namespace std; class T4Tutorials { public: int pyramid() { int i,j,s,rows; cout<<"enter no of rows"; cin>>rows; for(i=1; i<=rows; i++) { for(s=i; s<rows; s++) { cout<<" "; } for(j=1; j<=2*i-1; j++) { cout<<"*"; } cout<<"\n"; } } }; int main() { T4Tutorials myobj; myobj. pyramid(); } |
C++ pyramid pattern using inline function
Here, I am sharing you the pyramid pattern using the inline function by using C++.
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 |
#include<iostream> using namespace std; class even_number { private: int rows,sp,p,space,i; public : int input(); }; inline even_number::input() { cout<<"Enter the number of rows"<<endl; cin>>rows; { for(i=1; i<=rows; i++) { for(sp=i; sp<rows; sp++) { cout<<" "; } for(p=1;p<=(2*i-1);p++) { cout<<"*"; } cout<<endl; } } } int main() { even_number obj; obj.input(); } |
C++ pyramid pattern using friend function
Here, I am sharing you the pyramid pattern using the friend function by using C++.
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 |
#include<iostream> using namespace std; class even_number { public: int rows,sp,p,space,i; public : input() { cout<<"Enter the number of rows"<<endl; cin>>rows; } friend int view(even_number); }; int view(even_number u){ for(u.i=1; u.i<=u.rows; u.i++){ for(u.sp=u.i; u.sp<u.rows;u.sp++) { cout<<" "; } for(u.p=1;u.p<=(2*u.i-1);u.p++) { cout<<"*"; } cout<<endl; } } int main() { even_number u; u.input(); view(u); } |