Friday, April 19, 2019

How to make Pyramid in C++ Using Loop


Making Pyramid of Stars Numbers 

#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int row, i, j;

 cout<<"Enter Number of row : - ";
 cin>>row;
for (i = row; i >=1; --i){
for(j = 1; j <= i; ++j){
cout<<"* ";
}
cout<<"\n";
}
return 0;
}

Output

Enter Number of row : - 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


Making Pyramid of Stars * 

#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int row, i, j;

 cout<<"Enter Number of row : - ";
 cin>>row;
for (i = row; i >=1; --i){
for(j = 1; j <= i; ++j){
cout<<"* ";
}
cout<<"\n";
}
return 0;
}

Output

Enter Number of row : - 5
* * * * *
* * * *
* * *
* *
*


Making Pyramid of Stars Alphabet 

#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 char input, alp = 'A';
 int i, j;


 cout<<"Enter Char you wanna Print : - ";
for (i = 1; i <=(input-'A'+1); ++i){
 cin>>input;


  for(j = 1; j <= i; ++j){
  cout<<alp<<" ";
 }
 ++alp;
 cout<<"\n";
}
return 0;
}

Output


Enter Char you wanna Print : - G
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G

No comments:

Post a Comment