Given an integer N, the task is to print a pattern of N rows representing an inverted full pyramid. In this pattern, the first row has (2 * N – 1) stars, the second row has (2 * N – 3) stars, and so on until the Nth row, which has only 1 star. All stars are center-aligned.
Examples:
Input: 3
Output:
*****
***
*Input: 5
Output:
*********
*******
*****
***
*
Approach:
The problem can be solved using two nested loops inside another loop. The outer loop will run for the rows and the first inner loop will print the spaces and second loop will print stars. If we have an inverted full pyramid pattern with N rows, the 1st row will have 0 space followed by (2 * N – 1) stars, the 2nd row will have 1 space followed by (2 * N – 3) stars, the third row will have 2 spaces followed by (2 * N – 5) stars and so on. So, Nth row will have (N – 1) spaces followed by 1 star.
Step-by-step approach:
-
Run an outer loop from i = 1 to the number of rows N.
-
Run an inner loop from j = 1 to i – 1.
- Print an empty space (‘ ‘) in each iteration of the inner loop.
-
Run an inner loop from j = 1 to 2 * (N – i) + 1.
- Print an asterisk (‘*’) in each iteration of the inner loop.
- Print a newline character (“\n”) to move to the next row.
-
Run an inner loop from j = 1 to i – 1.
- After N iterations, we will have the inverted full pyramid pattern.
Below is the implementation of the above approach:
#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 'i - 1' spaces
for ( int j = 1; j <= i - 1; j++) {
cout << " " ;
}
// Inner loop prints '2 * (N - i) + 1' stars
for ( int j = 1; j <= 2 * (N - i) + 1; j++) {
cout << "*" ;
}
// Move to the next line
cout << "\n" ;
}
return 0;
} |
public class PyramidPattern {
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 'i - 1' spaces
for ( int j = 1 ; j <= i - 1 ; j++) {
System.out.print( " " );
}
// Inner loop prints '2 * (N - i) + 1' stars
for ( int j = 1 ; j <= 2 * (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 'i - 1' spaces
for j in range ( 1 , i):
print ( " " , end = "")
# Inner loop prints '2 * (N - i) + 1' stars
for j in range ( 1 , 2 * (N - i) + 2 ):
print ( "*" , end = "")
# Move to the next line
print ()
|
using System;
class Program {
static void 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 'i - 1' spaces
for ( int j = 1; j <= i - 1; j++) {
Console.Write( " " );
}
// Inner loop prints '2 * (N - i) + 1' stars
for ( int j = 1; j <= 2 * (N - i) + 1; j++) {
Console.Write( "*" );
}
// Move to the next line
Console.WriteLine();
}
}
} |
// Number of rows let N = 5; // Outer loop runs N times, once for each row for (let i = 1; i <= N; i++) {
// Inner loop prints 'i - 1' spaces
for (let j = 1; j <= i - 1; j++) {
process.stdout.write( " " );
}
// Inner loop prints '2 * (N - i) + 1' stars
for (let j = 1; j <= 2 * (N - i) + 1; j++) {
process.stdout.write( "*" );
}
// Move to the next line
console.log();
} |
********* ******* ***** *** *
Time Complexity: O(N^2), where N is the number of rows in the pattern.
Auxiliary Space: O(1)