1.
Introduction
2.
Star Half Pyramid
3.
Number Half Pyramid
3.1.
Repetitive numbers
3.2.
Non Repetitive numbers
4.
Alphabet Half Pyramid
4.1.
Repetitive alphabets
4.2.
Non - repetitive alphabets
5.
5.1.
What are the different types of loops?
5.2.
Can we use a while loop instead of for loop?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

# Different Half Pyramid Patterns

0 upvote

## Introduction

Half pyramid is a pattern, and it is in a triangle shape. In this blog, we will implement half pyramid of different types using the programming language C++. The design will be made using stars, numbers, and alphabets. But before moving on to the blog, first clearance of basic loops is essential. For that, you can refer to loops in C/C++.

## Star Half Pyramid

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<<*<<” “;
}
cout<<"\n";
}
}``````

Output:

``````*
* *
* * *
* * * *
* * * * *``````

## 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";
}
}``````

Output :

``````1
2 2
3 3 3
4 4 4 4
5 5 5 5 5``````

### 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";
}
}``````

Output:

``````1
1 2
1 2 3
1 2 3 4
1 2 3 4 5``````

## 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;
}``````

Output :

``````A
B B
C C C
D D D D
E E E E E``````

### 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;
}``````

Output :

``````A
A B
A B C
A B C D
A B C D E``````

### 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.