Java Program to Print Reverse Pyramid Star Pattern

17 Mar, 2021
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)
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 loopUsing for loopUsing do-while loop
Using while loop
Using for loop
Using do-while loop
Example 1: Using While Loop
Java
// Java program to Print Reverse Pyramid Star Pattern// Using While loop  // Importing input output classesimport java.io.*;  // Main classclass 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--;        }    }}
*************
 ***********
  *********
   *******
    *****
     ***
      *
Example 2: Using for Loop
Java
// Java program to print reverse pyramid star pattern// Using for loopimport 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
// Java program to Print Reverse Pyramid Star Pattern// Using do-while loop  // Importing input output classesimport java.io.*;  // Main Classpublic 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);    }}
 *************
  ***********
   *********
    *******
     *****
      ***
       *
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.