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:
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:
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:
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:
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!