Table of contents
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.
Frequently Asked Questions
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

Author Aditi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

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";
  }
}
You can also try this code with Online C++ Compiler
Run Code

Output:

*
* *
* * *
* * * *
* * * * *
You can also try this code with Online C++ Compiler
Run Code

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.

Live masterclass