Print Mirror Lower Star Triangle Pattern in Java



In this article, we will understand how to print a mirror lower star triangle pattern in Java. The program will use loops and print statements to get the output that we want, allowing us to get the pattern based on the number of rows specified by the user or predefined in the code.

Problem Statement

Write a Java program to print a mirror lower star triangle pattern. Below is a demonstration of the same ?

Input

Enter the number of rows : 8

Output

The mirror lower star triangle pattern : 
* * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *
     * * *
      * *
       *
       *
      * *
     * * *
    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
* * * * * * * *

Different Approaches

Below are the different approaches to print mirror lower star triangle patterns ?

Using user input approach

Following are the steps to print the mirror lower star triangle pattern ?

  • First, we will import the Scanner class from java.util package.
  • After that we will define the MirrorTriangle class and the main method.
  • Declare integer variables for loops and user input.
  • Create a Scanner object to read user input.
  • Prompt for and store the number of rows in my_input.
  • Print the header for the triangle pattern.
  • Use nested loops to print the upper triangle (add spaces and stars) and print the lower triangle (count down from my_input - 1).

Example

Here, the input is entered by the user based on a prompt ? 

import java.util.Scanner;
public class MirrorTriangle{
   public static void main(String args[]){
      int i, j, k, my_input;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number of rows : ");
      my_input = my_scanner.nextInt();
      System.out.println("The Mirror Lower Star Triangle pattern : ");
      for (i = 1; i <= my_input; i++) {
         for (j = 1; j < i; j++) {
            System.out.print(" ");
         }
         for (j = i; j <= my_input; j++) {
            System.out.print("*" + " ");
         }
         System.out.println();
      }
      for (i = my_input - 1; i >= 0; i--) {
         for (j = 0; j < i; j++) {
            System.out.print(" ");
         }
         for (j = i; j <= my_input - 1; j++) {
            System.out.print("*" + " ");
         }
         System.out.println();
      }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number of rows : 8
The Mirror Lower Star Triangle pattern :
* * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *
     * * *
      * *
       *
       *
      * *
     * * *
    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
* * * * * * * *

Using predefined input approach

Following are the steps to print the mirror lower star triangle pattern ?

  • First, we define the MirrorTriangle class and the main method.
  • Set my_input to 8.
  • Print the number of rows and will print the header for the triangle pattern.
  • Use nested loops to print the upper triangle (add spaces and stars) and print the lower triangle (count down from my_input - 1).

Example

Here, the integer has been previously defined, and its value is accessed and displayed on the console ?

public class MirrorTriangle{
   public static void main(String args[]){
      int i, j, k, my_input;
      my_input = 8;
      System.out.println("The number of rows is defined as " +my_input);
      System.out.println("The mirror lower star triangle pattern : ");
      for (i = 1; i <= my_input; i++) {
         for (j = 1; j < i; j++) {
            System.out.print(" ");
         }
         for (j = i; j <= my_input; j++) {
            System.out.print("*" + " ");
         }
         System.out.println();
      }
      for (i = my_input - 1; i >= 0; i--) {
         for (j = 0; j < i; j++) {
            System.out.print(" ");
         }
         for (j = i; j <= my_input - 1; j++) {
            System.out.print("*" + " ");
         }
         System.out.println();
      }
   }
}

Output

The number of rows is defined as 8
The mirror lower star triangle pattern :
* * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *
     * * *
      * *
       *
       *
      * *
     * * *
    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
* * * * * * * *
Updated on: 2024-10-15T11:56:29+05:30

643 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements