Solution in C
Let's see the solution of the inverted pant's style pattern program in C.
Code:
// Include header file
#include <stdio.h>
int main() {
int n = 5;
for (int row = n; row >= 0; row--) {
// Prints first half of the stars
for (int col = n; col > row; col--) {
printf("* ");
}
// Prints space in between
for (int col = 1; col <= 4 * row; col++) {
printf(" ");
}
// Prints second half of the stars
for (int col = row + 1; col <= n; col++) {
printf("* ");
}
// Goes to a new line
printf("\n");
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:

Solution in C++
Let's see the solution of the inverted pant's style pattern program in C++.
Code:
// Include header file
#include <iostream>
int main() {
int n = 5;
for (int row = n; row >= 0; row--) {
// Prints first half of the stars
for (int col = n; col > row; col--) {
std::cout << "* ";
}
// Prints space in between
for (int col = 1; col <= 4 * row; col++) {
std::cout << " ";
}
// Prints second half of the stars
for (int col = row + 1; col <= n; col++) {
std::cout << "* ";
}
// Goes to a new line
std::cout << std::endl;
}
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

To learn C++, check out our C++ guided path.
Solution in Java
Let's see the solution of the inverted pant's style pattern program in Java.
Code:
class Main {
public static void main(String[] args) {
int n = 5;
for(int row = n ; row >= 0 ; row--) {
// Prints first half of the stars
for(int col = n ; col > row ; col--) {
System.out.print("* ");
}
// Prints space in between
for(int col = 1 ; col <= 4 * row ; col++) {
System.out.print(" ");
}
// Prints second half of the stars
for(int col = row + 1 ; col <= n ; col++) {
System.out.print("* ");
}
// Goes to a new line
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output:

To learn Java, check out our Java guided path.
Solution in Python
Let's see the solution of the inverted pant's style pattern program in Python.
Code:
n = 5
row = n
while (row >= 0) :
# Prints first half of the stars
col = n
while (col > row) :
print("* ", end ="")
col -= 1
# Prints space in between
col = 1
while (col <= 4 * row) :
print(" ", end ="")
col += 1
# Prints second half of the stars
col = row + 1
while (col <= n) :
print("* ", end ="")
col += 1
# Goes to a new line
print()
row -= 1

You can also try this code with Online Python Compiler
Run Code
Output:

To learn Python, check out our Python guided path.
Complexity
Let's discuss the time complexity and space complexity of the above approach.
Time complexity
The time complexity is O(n2) as nested loops are used here.
Space Complexity
The space complexity is O(1) as no extra space is used in the above program.
Now that you have learned how to solve the inverted pant's style pattern program check out the Pant's Style Pattern program.
FAQs
-
What is a pattern program?
Pattern programs consist of numbers, alphabets, or symbols in a particular form. The pattern programs can be solved easily using loops like the for loop, while loop, etc.
-
Why are pattern programs asked in interviews?
Pattern programs are asked in interviews to judge the candidate's problem-solving ability and programming knowledge.
-
Which are the commonly asked pattern-solving problems in interviews?
In interviews, the commonly asked pattern-solving problems are Hollow Diamond Star Pattern, Inverted V-shape Number Pattern, Matrix in Reverse Spiral Form, etc.
Key Takeaways
In this article, we have extensively discussed the inverted pant's style pattern program and the solution in various languages like C, C++, Java, and Python.
We hope that this blog has helped you enhance your knowledge regarding the inverted pant's style pattern program, and if you would like to learn more, check out our Mock Test Series and Coding Interview Questions. Do upvote our blog to help other ninjas grow. Happy Coding!