Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
3.1.
C
3.2.
C++
3.3.
Java
3.4.
Python
4.
Frequently Asked Questions
4.1.
What is the Average Time Complexity of Printing an Inverted Half Pyramid?
4.2.
What is the Space Complexity of Printing an Inverted Half Pyramid?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Inverted Half Pyramid

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

Introduction

The best way to learn how looping structures in general-purpose programming languages function is to use pattern programs. It assists newcomers in visualizing how each loop iteration works. Pattern programs are simply patterns made up of integers, alphabets, or symbols in a specific order. These kinds of pattern programs are simple to solve when employing the for loop condition. These are often asked questions in campus placements and career interviews.

Problem Statement

Create a program that prints Inverted Half Pyramid.

Check out the blog on the Fibonacci Series in Java, to know more about it.

Solution

There can be two or more loops in each pattern program. The number of loops required is determined by the pattern's complexity. The first loop affects the row, whereas the second affects the column.

C

The following code illustrates printing the Inverted Half Pyramid in C :

#include <stdio.h>
int main() {
    int length=5;
        for(int a=length;a>0;a--) //loop for printing rows
        {
            for(int b=length-1;b>=a;b--) //loop for printing spaces
            {
                printf(" ");
            }
            for(int b=1;b<=a;b++) //loop for printing columns
            {
                printf("%d",b);
            }
            printf("\n"); //changing line
        }
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:
Output

C++

The following code illustrates printing Inverted Half Pyramid in C++:

#include <iostream>
using namespace std;
int main() {
    int length=5;
        for(int a=length;a>0;a--) //loop for printing rows
        {
            for(int b=length-1;b>=a;b--) //loop for printing spaces
            {
                cout<<" ";
            }
            for(int b=1;b<=a;b++) //loop for printing columns
            {
                cout<<b;
            }
            cout<<endl; //changing line
        }
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Output

Java

The following code illustrates printing Inverted Half Pyramid in Java:

public class Ninja {
    public static void main(String[] args) {
        int length=5;
        for(int a=length;a>0;a--) //loop for printing rows
        {
            for(int b=length-1;b>=a;b--) //loop for printing spaces
            {
                System.out.print(" ");
            }
            for(int b=1;b<=a;b++) //loop for printing columns
            {
                System.out.print(b);
            }
            System.out.println(); //changing line
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Output

Python

The following code illustrates printing the Inverted Half Pyramid in Python:

length=5;
for a in range(length,0,-1):
    for b in range(length,a,-1):
        print(" ",end="")
    for b in range(1,a+1):
        print(b,end="")
    print("")
You can also try this code with Online Python Compiler
Run Code

Output:

Output

Frequently Asked Questions

What is the Average Time Complexity of Printing an Inverted Half Pyramid?

The Time Complexity of printing an Inverted Half Pyramid is O(n2).

What is the Space Complexity of Printing an Inverted Half Pyramid?

The Space Complexity of printing an Inverted Half Pyramid is O(1).

Conclusion

In this article, we have extensively discussed printing Inverted Half Pyramid and its implementation in different programming languages. The article explains the implementation in various programming languages like C, C++, Java, and Python. There are many more approaches, but these are the most efficient in time and space.
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.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass