Given an integer N, print N rows of an inverted right half pyramid pattern. In an inverted right half pattern of N rows, the first row has N number of stars, the second row has (N - 1) number of stars, and so on till the Nth row, which has only 1 star.

Examples:
Input: n = 5
Output:
*****
****
***
**
*Input: n = 3
Output:
***
**
*
Approach
- Follow these steps to print the inverted right half pyramid pattern using two nested loops.
- Run the outer loop from i = 1 to N to control the rows.
- For each iteration of the outer loop, run the inner loop from j = 1 to N - i + 1.
- Print an asterisk (*) in each iteration of the inner loop.
- Print a newline after completing the inner loop.
- Continue until all N rows are printed, forming the inverted right half pyramid.
#include <iostream>
using namespace std;
int main()
{
// Number of rows
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++) {
// Inner loop prints 'N - i + 1' stars
for (int j = 1; j <= N - i + 1; j++) {
cout << "*";
}
// Move to the next line
cout << "\n";
}
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
// Number of rows
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++) {
// Inner loop prints 'N - i + 1' stars
for (int j = 1; j <= N - i + 1; j++) {
System.out.print("*");
}
// Move to the next line
System.out.println();
}
}
}
# Number of rows
N = 5
# Outer loop runs N times, once for each row
for i in range(1, N + 1):
# Inner loop prints 'N - i + 1' stars
for j in range(1, N - i + 2):
print("*", end="")
# Move to the next line
print()
using System;
class MainClass
{
public static void Main(string[] args)
{
// Number of rows
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++)
{
// Inner loop prints 'N - i + 1' stars
for (int j = 1; j <= N - i + 1; j++)
{
Console.Write("*");
}
// Move to the next line
Console.WriteLine();
}
}
}
// Number of rows
const N = 5;
// Outer loop runs N times, once for each row
for (let i = 1; i <= N; i++) {
// Inner loop prints 'N - i + 1' stars
for (let j = 1; j <= N - i + 1; j++) {
process.stdout.write("*");
}
// Move to the next line
process.stdout.write("\n");
}
Output
***** **** *** ** *
Time Complexity: O(N^2), where N is the number of rows in the pattern.
Auxiliary Space: O(1)