Codeforcoding

Hollow Pyramid Pattern in Cpp using nested while loop

Hollow Pyramid Pattern in Cpp using nested while loop

In this tutorial, we will discuss Hollow Pyramid Pattern in Cpp using nested while loop

In this program, we are going to learn about how to display  Hollow Pyramid Pattern using nested while loop in C++ language

Here, we display the Hollow Pyramid Pattern program with coding using nested while loop and also program get input from the user using cin function in C++  language

The user can provide numbers as they wish and get the  Hollow pyramid pattern according to their input.

Hollow Pyramid Pattern 1

Hollow Pyramid triangle star pattern

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int rows;
    cout<<"Enter the number of rows to Pyramid: ";
    cin>>rows;
    int i,j,k;
    i=1;
    while(i<=rows){
        j=i;
        while(j<rows){
        cout<<" ";
        j++;
    }

    k=1;
while(k<2*i){
    if(i==rows || (k==1 || k==2*i-1))
        cout<<"*";
    else
        cout<<" ";
    k++;
}
i++;
cout<<"\n";//Move to the next line for print
    }
getch();
return 0;
}

When the above code is executed, it produces the following results

Hollow Pyramid Pattern
Hollow Pyramid Pattern in C++

 

Hollow Pyramid Pattern 2

Inverted Hollow Pyramid triangle star pattern

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int rows;
    cout<<"Enter the number of rows to Pyramid: ";
    cin>>rows;
    int i,j,k;
    i=rows;
    while(i>=1){
        j=rows;
        while(j>i){
        cout<<" ";
        j--;
    }

    k=1;
while(k<2*i){
    if(i==rows || (k==1 || k==2*i-1))
        cout<<"*";
    else
        cout<<" ";
    k++;
}
i--;
cout<<"\n";//Move to the next line for print
    }
getch();
return 0;
}

When the above code is executed, it produces the following results

Pyramid Pattern in Cpp

Suggested for you

Operator in C++ language

Data type in C++ language

While loop in C++ language

Nested while loop in C++ language

 

Hollow Pyramid Pattern in Java using nested while
Hollow Pyramid Pattern in C using nested while
Exit mobile version