1.
Introduction
2.
Problem Statement
3.
Solution
3.1.
C
3.2.
C++
3.3.
Java
3.4.
Python
4.
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

Nagendra
0 upvote

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

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

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

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("")``````

Output:

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