In this article, we will learn how to Print the Half Pyramid Pattern of start in C++ language. We will try to use two different techniques to print this pattern.
- Using normal for loop to print Pyramid pattern.
- Using decrement Operator in for loop to print Pyramid pattern.
1. Print Half Pyramid Pattern of start in C++ language using for loop
Desired Output:
*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Please enter the size for Pyramid: ";
cin>>num;
for(int row=1;row<=num;row++)
{
for(int col=1;col<=row;col++)
{
cout<<"* ";
}
cout<<"\n";
}
return 0;
}
Actual Output:
Please enter the size for Pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
2. Using decrement Operator in for loop to print Pyramid pattern
In this example, we are using the for loop with a decrement operator for the Pyramid pattern print.
split Pandas DataFrame column by single Delimiter
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Please enter the size for Pyramid: ";
cin>>num;
for(int i=num; i>=1; i--)
{
for(int j=i;j<=num;j++)
{
cout<<"* ";
}
cout<<"\n";
}
return 0;
}
Output
Please enter the size for Pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
In this example, we are using the for loop with decrement operator in the inner for loop to print the Pyramid pattern.
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=i;j>=1;j--)
{
cout<<"* ";
}
cout<<"\n";
}
return 0;
}
Output
*
* *
* * *
* * * *
* * * * *