Java program to print inverted Pyramid patterns in different ways

Inverted inverted pyramid pattern program in Java:

Let’s learn how to print an inverted pyramid pattern in Java. I will show you how to write the algorithm and how to write the program in Java to print this pattern. Before we move to the programming part, let me show you how the algorithm works.

Inverted pyramid pattern:

The following inverted pyramid pattern is of height 5:

*********
 *******
  *****
   ***
    *

This is printed with blank spaces and stars. We can use two loops to print this pattern. One loop will run inside another loop. The outer loop will point to each row of the pattern and the inner loop will print the row stars.

Let me replace all blank spaces with # in the above pattern:

*********
#*******
##*****
###***
####*
  • The height of the pattern is 5.
  • For the first row, it prints 0 blank spaces and 9 stars.
  • For the second row, it prints 1 blank space and 7 stars.
  • For the third row, it prints 2 blank spaces and 5 stars.

So, the number of blank spaces keep increasing by one from 0 on each row and the stars are printed for 2*(height - i) + 1 times, where i is the row number and it starts from 1.

Let’s write down the program with this algorithm.

Example 1: Java program to print inverted pyramid pattern with for loops:

The following Java program prints an inverted pyramid pattern of a given height:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int height, i, j;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the height of the pattern: ");
        height = scanner.nextInt();

        for (i = 1; i <= height; i++) {
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }

            for (j = 1; j <= 2 * (height - i) + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Here,

  • The integer variable height is used to keep the height of the pattern. It uses a Scanner object to read the height.
  • The for loop runs from i = 1 to i = height. On each iteration, it runs two more loops to print the blank spaces and stars.
    • The first for loop runs for i - 1 number of times to print the blank spaces.
    • The second for loop runs for 2*(height - i) + 1 number of times to print the stars.
  • It prints a new line at the end of each iteration of the loop.

It will give output as below:

Enter the height of the pattern: 
5
*********
 *******
  *****
   ***
    *

Enter the height of the pattern: 
10
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

Example 2: Java program to print inverted pyramid pattern with while loops:

The above program uses for loops to print the pattern. We can also use any other loops to print the same pattern. Let’s use while loops to write the same algorithm:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int height, i = 1, j;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the height of the pattern: ");
        height = scanner.nextInt();

        while (i <= height) {
            j = 1;
            while (j < i) {
                System.out.print(" ");
                j++;
            }

            j = 1;
            while (j <= 2 * (height - i) + 1) {
                System.out.print("*");
                j++;
            }
            i++;
            System.out.println();
        }
    }
}

We have to initialize the variables before the loops start and increment the variable at the end of each iteration. The while loop checks a condition and executes its body until the condition is true.

It will print similar results.

Enter the height of the pattern: 
7
*************
 ***********
  *********
   *******
    *****
     ***
      *

Java Example program to print inverted pyramid

You might also like: