This is once of the basic program to start programming in C++.Here your main goal is to print the number’s in pyramid shape.The size of the shape depends on the input of the user.The output is similar to this picture:
For this we need to write the control statements such as for loop.Here in the program we use the method print_pyramid to print the output.Now we shall write a program to this problem:
#include<iostream.h> #include<conio.h> class pyramid { public: int n; void print_pyramid(); }; void pyramid :: print_pyramid() { for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { cout<<j<<" "; } cout<<"\n"; } } void main() { clrscr(); cout<<"Enter number of lines to be printed in pyramid shape:"; pyramid ob; cin>>ob.n; ob.print_pyramid(); getch(); }
Output:
you can download the code:download