3 ways in Java to print a reverse half pyramid pattern

Java program to print a reverse half pyramid pattern:

In this post, we will learn to print a reverse half pyramid pattern in Java. We will learn how to write Java program to print the reverse half pyramid with star or any other character or using numbers.

The user will enter the height of the pattern and it will print the pattern of that height.

We will have to use two loops to print the pattern. The outer loop will point to each row of the pattern and the inner loop will print the content of each row. You will find the program easy if you understand how the inner loop works.

Algorithm to print a reverse half pyramid:

We can use the below algorithm to print a reverse half pyramid:

  • Take the height of the pattern as an input from the user.
  • Run one loop from i = 1 to i = height.
  • On each iteration of the loop, run another inner loop from j = 1 to j = height - i + 1.
  • Print the character inside the inner loop. At the end of each iteration of the outer loop, print a newline character to move the control to the next line.

We will add a blank space at the end of the character while printing it. This same algorithm can be used to print the pyramid of numbers.

Example 1: Java program to print a reverse half pyramid with star:

The following program prints a half pyramid with star:

import java.util.Scanner;

class Main {

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

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

        for (int i = 1; i <= height; i++) {
            for (int j = 1; j <= height - i + 1; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}
  • It takes the height of the pattern as an input from the user and assign it to the height variable. We are using a Scanner object to read the user-input integer.
  • Two for loops are used to print the pattern.
    • The outer for loop runs from i = 1 to i = height and the inner for loop runs from j = 1 to j = height - i + 1.
    • It prints the character inside the inner loop.
  • At the end of each iteration of the outer for loop, it prints a new line.

If you run this program, it will print print output as below:

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

Enter the height of the pattern: 
6
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Example 2: Java program to print a reverse half pyramid with while loops:

Let’s replace the for loops with while loops in the above program. It will work in a similar way to print the pattern.

Below is the complete program:

import java.util.Scanner;

class Main {

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

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

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

Here,

  • The for loops are replaced with while loops.
  • The values of i and j are initialized before each of these loops starts.
  • The i and j are incremented at the end of each iteration.

It will print similar results.

Java example program to print a reverse half pyramid pattern

Example 3: Java program to print a reverse half pyramid of numbers:

To print a pyramid of numbers, we will have to print numbers instead of a star in the inner loop. We can print the value of i or j or a constant number or a number that increments always.

For example,

import java.util.Scanner;

class Main {

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

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

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

This program prints the value of j and it will print output as below:

Enter the height of the pattern: 
5
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

If I change the for loop to print the value of i,

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

It will print output as below:

Enter the height of the pattern: 
5
1 1 1 1 1 
2 2 2 2 
3 3 3 
4 4 
5 

You might also like: