What is Pyramid Pattern And Hollow Pyramid Pattern?
(i) Pyramid Pattern
Program:
#include <iostream>
using namespace std;
int main()
{
int n; //Input number of rows
cout<<"Enter no. of rows: ";
cin>>n;
for(int i=0;i<n;i++) //loop to terminate line
{
for(int j=0;j<n-i-1;j++) //loop to print spaces to make the pyramid pattern
cout<<" ";
for(int k=0;k<i+1;k++) //loop to print *
{
cout<<"* "<<" ";
}
cout<<endl;
}
return 0;
}
Output:
*
* *
* * *
* * * *
* * * * *
(ii) Hollow Pyramid Pattern
Program:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter no. of rows: ";
cin>>n;
for(int i=0;i<n;i++) //loop to terminate line
{
for(int j=0;j<n-i-1;j++) //loop to print spaces
cout<<" ";
for(int k=0;k<i+1;k++) //loop to print * at boundaries and spaces in rest of index
{
if(i==0 || k==0 || i==n-1 || k==i)
cout<<"* "<<" ";
else
cout<<" "<<" ";
}
cout<<endl;
}
return 0;
}
Output:
*
* *
* *
* *
* * * * *
C++ Program To Print Pyramid Pattern and Hollow Pyramid Pattern
Reviewed by Beast Coder
on
February 10, 2021
Rating:
No comments: