Signup/Sign In
LAST UPDATED: MARCH 14, 2022

C++ Program To Print Half Pyramid

Here our task is to print the required pattern without actually writing it manually. We will see how to do this for a half pyramid pattern. The simplest case will be to make the pattern using * only.

Following is the program to

Print Half Pyramid Using *

#include <iostream>
using namespace std;

int main()
{
    int rows;

    cout << "Enter number of rows: ";
    cin >> rows;

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


Enter number of rows: 6
*
* *
* * *
* * * *
* * * * *
* * * * * *

Now we will proceed towards a little difficult task: arrange the numbers in the form of a half pyramid using the same concept.

?
#include<iostream.h>

int main()
{
    int rows;

    cout << "Enter number of rows: ";
    cin >> rows;

    for(int i = 1; i <= rows; ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << j<< " ";
        }
        cout << "\n";
    }
getch();
return 0 ;
}
?


Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Conclusion

As for the implementation part, we can use alphabets, or any other symbol for the pattern but the general will remain the same for always.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.