Approach:
1. Get the number of input rows from the user using Scanner Class or BufferedReader Class object.
2. Now run two loops
- Outer loop to iterate through a number of rows as initialized or input is taken from reader class object in java. Now,
- Run an inner loop from 1 to ‘i-1’
- Ru another inner loop from 1 to rows * 2 – (i × 2 – 1)
Illustration:
Input: number = 7 Output: ************* *********** ********* ******* ***** *** *
Methods: We can print a reverse pyramid star pattern using the following methods:
- Using while loop
- Using for loop
- Using do-while loop
Example 1: Using While Loop
// Java program to Print Reverse Pyramid Star Pattern // Using While loop // Importing input output classes import java.io.*;
// Main class class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variable to
// Size of the pyramid
int number = 7 ;
int i = number, j;
// Nested while loops
// Outer loop
// Till condition holds true
while (i > 0 ) {
j = 0 ;
// Inner loop
// Condition check
while (j++ < number - i) {
// Print whitespaces
System.out.print( " " );
}
j = 0 ;
// Inner loop
// Condition check
while (j++ < (i * 2 ) - 1 ) {
// Print star
System.out.print( "*" );
}
// By now, we reach end of execution for one row
// so next line
System.out.println();
// Decrementing counter because we want to print
// reverse of pyramid
i--;
}
}
} |
************* *********** ********* ******* ***** *** *
Steps to solve this problem:
1. Initialize the size of the pyramid ‘number =7’ and the variables ‘i’ and ‘j’.
2. The outer loop will run through rows of the pyramid with ‘i’ starting from number and decrementing by 1 in each iteration.
3. First inner loop will start to print the gaps in each row with ‘j’ starting from ‘i’ and incrementing by 1 until it reaches number.
4. Now start second inner loop with ‘j’ starting at 1 and increment it by 1 until it reaches (2 * i – 1) to print the stars in each row.
5. Print new line to end each row, then repeat steps 3 through 5 until the outer loop ends.
Example 2: Using for Loop
// Java program to print reverse pyramid star pattern // Using for loop import java.io.*;
class GFG{
public static void main (String[] args)
{ // Size of the pyramid
int number = 7 ;
int i, j;
// Outer loop handle the number of rows
for (i = number; i >= 1 ; i--)
{
// Inner loop print space
for (j = i; j < number; j++)
{
System.out.print( " " );
}
// Inner loop print star
for (j = 1 ; j <= ( 2 * i - 1 ); j++)
{
System.out.print( "*" );
}
// Ending line after each row
System.out.println( "" );
}
} } |
************* *********** ********* ******* ***** *** *
Example 3: Using do-while Loop
// Java program to Print Reverse Pyramid Star Pattern // Using do-while loop // Importing input output classes import java.io.*;
// Main Class public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declare and initialize variable to
// Size of the pyramid
int number = 7 ;
int i = number, j;
// Outer loop iterate until i > 0 is false
do {
j = 0 ;
// First inner do-while loop
do {
// Prints space until j++ < number - i is
// false
System.out.print( " " );
} while (j++ < number - i);
j = 0 ;
// Second inner do-while loop
// Inner loop prints star
// until j++ < i * 2 - 2 is false
do {
// print star
System.out.print( "*" );
}
while (j++ < i * 2 - 2 );
// Print whitespace
System.out.println( "" );
}
// while of outer 'do-while' loop
while (--i > 0 );
}
} |
************* *********** ********* ******* ***** *** *
Complexity Analysis :
Time Complexity : O(N^2)
Space Complexity : O(1)