C++ program to print number in pyramid shape

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:
pyramid
you can download the code:download

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in C++ and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s