Number Half Pyramid
Repetitive numbers
Problem Statement: Write a program to print a half pyramid where numbers are repetitive in C++.
Solution:
#include <iostream>
using namespace std;
int main()
{
int row = 5;
for(int i=1;i<=row;i++)
{
for(int j=1;j<=i;j++)
{
cout<<i<<” “;
}
cout<<"\n";
}
}
You can also try this code with Online C++ Compiler
Run Code
Output :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
You can also try this code with Online C++ Compiler
Run Code
Non Repetitive numbers
Problem Statement: Write a program to print a half pyramid where numbers are non-repetitive in C++.
Solution:
#include <iostream>
using namespace std;
int main()
{
int row = 5;
for(int i=1;i<=row;i++)
{
for(int j=1;j<=i;j++)
{
cout<<j<<” “;
}
cout<<"\n";
}
}
You can also try this code with Online C++ Compiler
Run Code
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
You can also try this code with Online C++ Compiler
Run Code
Alphabet Half Pyramid
Repetitive alphabets
Problem Statement: Write a program to print a half pyramid where alphabets are repetitive in C++.
Solution:
#include <iostream>
using namespace std;
int main()
{
int row = 5;
int letter = 65;
for (int i = 0; i < row; i++) {
for (int j = 0; j <= i; j++) {
char ch = char(letter);
cout << ch << " ";
}
letter = letter + 1;
cout << endl;
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output :
A
B B
C C C
D D D D
E E E E E
You can also try this code with Online C++ Compiler
Run Code
Non - repetitive alphabets
Problem Statement: Write a program to print a half pyramid where alphabets are non-repetitive in C++.
Solution:
#include <iostream>
using namespace std;
int main()
{
int row = 5;
for (int i = 0; i < row; i++) {
int letter = 65;
for (int j = 0; j <= i; j++) {
char ch = char(letter);
cout << ch << " ";
letter = letter + 1;
}
cout << endl;
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output :
A
A B
A B C
A B C D
A B C D E
You can also try this code with Online C++ Compiler
Run Code
Frequently Asked Questions
What are the different types of loops?
There are two types of loops: entry control and exit control loops. For and while loop is entry control and do while loop is exit control loop.
Can we use a while loop instead of for loop?
Yes, we can use a while loop instead of for loop, but while doing pattern problems a loop is preferred.
Conclusion
In this article, we have extensively discussed the implementation of different half pyramids in the C++ programming language.
Recommended Readings:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.
Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.