C++ Program to Print Pyramids and Patterns

Here you can learn lots of such pattern problems. These questions mostly asked in an MNC job interview or in their recruitment. Before seeing the solution to these questions first try to solve oneself.

1. Write a program to print pattern half pyramid I.

*
* *
* * *
* * * *
* * * * *
#include 

using namespace std;

int main()
{
    int n;
    cout<<"Enter the Number of row";    
    cin>>n;
    // this loop go row wise
    for(int i =0;i<n;i++)
    {
        // this loop print coloumn wise how many * print in row
        for(int j=0 ;j<=i;j++)
          {
              cout<<"*";
          }
        cout<<"\n";
    }
    

    return 0;
}

2. Write a program to print pattern half pyramid II.

* * * * *
* * * *
* * *
* *
*

 

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout<<"Enter the Number of row";
    cin>>n;
    // this loop go row wise
    for(int i =0;i<n;i++)
    {
        // this loop print coloumn wise how many * print in row
        for(int j=0 ;j<n-i;j++)
          {
              cout<<"*";
          }
        cout<<"\n";
    }
    

    return 0;
}

3. Write a program to print the pattern Full pyramid.

                *
              * * * 
            * * * * *
          * * * * * * *

 

#include <iostream>

using namespace std;

int main()
{
    int a,b,k;
    int row;
    cout<<"Enter the number of row";
    cin>>row;
    for(a=1;a<=row;a++,k=0)
    {
        // this loop for print space in pyramid
        for(b=1;b<=row-a;b++)
        {
            cout<<" ";
        }
        //this loop for print star in pyramid
        //if a and b starts with zero then while loop go in infinite
        while(k!=2*a - 1)
        {
            cout<<"* ";
            ++k;
        }
        cout<<endl;
    }
    

    return 0;
}

 

4. Write a program to print the pattern inverted Full pyramid.

* * * * * * *
  * * * * *
    * * *
      *
#include <iostream>

using namespace std;

int main()
{
    int a,b,k;
    int row;
    cout<<"Enter the number of row ";
    cin>>row;
    for(a=row;a>=1;a--)
    {
        // this loop for print space in pyramid
        for(b=1;b<=row-a;b++)
        {
            cout<<" ";
        }
        //this loop for print star in pyramid
       for(b=a;b<=2*a-1;b++)
        {
            cout<<"* ";
            
        }
        for(b=0;b<a-1;b++)
        {
            cout<<"* ";
        }
        cout<<endl;
    }
    

    return 0;
}

 

Leave a Reply

%d bloggers like this: