Java Program to Print Reverse Pyramid Star Pattern

Last Updated : 7 Aug, 2026
A Reverse Pyramid Star Pattern is a commonly asked Java pattern program that helps in understanding nested loops, spacing, and pattern generation. In this pattern, the number of stars decreases with each row while the leading spaces increase, creating an inverted pyramid.
  • The number of leading spaces increases by one in every row.
  • Useful for practicing loop control and pattern-based problems.

Illustration:

invertedFullPyramid

Approach

  • Initialize the number of rows.
  • Use an outer loop to print each row.
  • Print the required leading spaces.
  • Print the stars using the formula (2 × row − 1).
  • Reduce the number of stars after every row.
  • Move to the next line after completing each row.

Methods:

  • Using while loop
  • Using for loop
  • Using do-while loop

Example: Using While Loop

Java
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--;
        }
    }
}

Output
*************
 ***********
  *********
   *******
    *****
     ***
      *

Explanation: The program starts by printing the maximum number of stars in the first row. With each iteration, one leading space is added and two stars are removed, resulting in a symmetric reverse pyramid pattern.

Example: Using for Loop

Java
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("");
    } 
}
}

Output
*************
 ***********
  *********
   *******
    *****
     ***
      *

Explanation: The outer for loop controls the rows, the first inner loop prints leading spaces, and the second inner loop prints the required number of stars. The number of stars decreases by two in each row until only one star remains.

Example 3: Using do-while Loop

Java
// 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);
    }
}
Try It Yourself
redirect icon

Output
 *************
  ***********
   *********
    *******
     *****
      ***
       *

Explanation: The do-while loop ensures that each row is printed at least once. Separate do-while loops print the leading spaces and stars, producing the same reverse pyramid pattern as the other approaches.

Comment