In this article, we are going to learn about how to print half Pyramid and full Pyramid patterns in C++ language. First, we are going to print the half Pyramid and then in the next program, we will print the full Pyramid.
- How to print half Pyramid using * in C++ language.
- How to print full Pyramid using * in C++ language.
How to print half Pyramid using * in C++ language.
In this example, we are going to print the half * pyramid using C++ language. We will make use of the for loops.
Desired Output for Size 5:
*
**
***
****
*****
C++ Program to print half Pyramid pattern
#include <iostream>
using namespace std;
int main()
{
int num, row, col;
cout<<"Please enter the number of rows: ";
cin>>num;
for(row = 1; row <= num; row++)
{
for(int s=1;s<=num-row;s++)
cout<<" ";
for(col = 1;col <= row; col++)
cout<<"*";
cout<<"\n";
}
return 0;
}
Output
Please enter the number of rows: 5
*
**
***
****
*****
2. C++ Program to print pyramid of stars,
In this example, we are going to print the full * pyramid using C++ language. We will make use of the for loops.
Desired Output for Size 5:
*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;
int main()
{
int num, row, col;
cout<<"Enter number of rows: ";
cin>>num;
for(row = 1; row <= num; row++)
{
for(int s=1;s<=num-row;s++)
cout<<" ";
for(col=1;col<=row;col++)
cout<<"* ";
cout<<"\n";
}
return 0;
}
Output
Enter number of rows: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *