[Solved] Hollow Pyramid with C++

Hollow Pyramid: The much awaited event at the entertainment industry every year is the “Screen Awards”. This year the event is going to be organized on December 25 to honour the Artists for their professional excellence in Cinema. The Organizers of the event, J&R Events, decided to design some attractive and LED Matrix panel boards for the show promotions all across the venue.
 
The Event organizers wanted to program the display boards with some specific pattern using alphabets and special characters. Help them write a program to design the pattern of a hollow pyramid in the matrix panel, given the number of lines of the pattern.
 
Input Format:
First line of the input is an integer that refers to the number of lines in the pattern.

Output Format:
Output the pattern as given in the output.
Refer sample input and output for formatting specifications.

Sample Input 1:

4

Sample Output 1:

bbb*bbb
bb*i*bb
b*iii*b
*******

Sample Input 2:

5

Sample Output 2:

bbbb*bbbb
bbb*i*bbb
bb*iii*bb
b*iiiii*b
*********

Solution


#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int si = n + (n - 1);
    int sp = n;
    int st = -1;
    for (int i = 1; i <= n; i++)
    {
        sp = sp - 1;
        st = st + 2;
        for (int j = 1; j <= sp; j++)
        {
            cout << "b";
        }
        for (int k = 1; k <= st; k++)
        {
            if (i > 1 && i < n)
            {
                if (k > 1 && k < st)
                {
                    cout << "i";
                }
                else
                {
                    cout << "*";
                }
            }
            else
            {
                cout << "*";
            }
        }
        for (int l = 1; l <= sp; l++)
        {
            cout << "b";
        }
        cout << endl;
    }
    // sauravhathi
    return 0;
}

import java.util.*;
class Main
{
public
    static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int si = num + (num - 1);
        int sp = num;
        int st = -1;
        for (int i = 1; i <= num; i++)
        {
            sp = sp - 1;
            st = st + 2;
            for (int j = 1; j <= sp; j++)
            {
                System.out.print("b");
            }
            for (int k = 1; k <= st; k++)
            {
                if (i > 1 && i < num)
                {
                    if (k > 1 && k < st)
                    {
                        System.out.print("i");
                    }
                    else
                    {
                        System.out.print("*");
                    }
                }
                else
                {
                    System.out.print("*");
                }
            }
            for (int l = 1; l <= sp; l++)
            {
                System.out.print("b");
            }
            System.out.println();
            //sauravhathi
        }
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *