Print Left Triangle Star Pattern in Java



In this article, we will understand how to print the left triangle star pattern using Java. The pattern is formed by using multiple for-loops and print statements.

Problem Statement

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

Input

Enter the number of rows : 8

Output

The right triangle star pattern : 
                 * 
               * * 
             * * * 
           * * * * 
         * * * * * 
       * * * * * * 
     * * * * * * * 
   * * * * * * * * 

Different approaches

Following are the steps to print the left triangle star pattern using Java ?

Using predefined input

Following are the steps to print the left triangle star pattern ?

  • Define an integer variable for the row count.
  • Use nested for loops to print spaces and stars for each row in a left triangle pattern.

Example

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

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

Output

The number of rows is defined as 8
The right triangle star pattern : 
                 * 
               * * 
             * * * 
           * * * * 
         * * * * * 
       * * * * * * 
     * * * * * * * 
   * * * * * * * * 

Using user-defined input

Following are the steps to print the left triangle star pattern ?

  • Import the Scanner class from java.util package to accept user input.
  • Create a Scanner object and ask the user to enter the number of rows.
  • Use nested for loops to control the rows, spaces, and stars printed in each row.

Example

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

import java.util.Scanner;
public class RightTriangle{
   public static void main(String args[]){
      int i, j, 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 right triangle star pattern : ");
     for (i=0; i<my_input; i++){
        for (j=2*(my_input-i); j>=0; j--){
           System.out.print(" ");
        }
        for (j=0; j<=i; 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 right triangle star pattern : 
                 * 
               * * 
             * * * 
           * * * * 
         * * * * * 
       * * * * * * 
     * * * * * * * 
   * * * * * * * * 
Updated on: 2024-11-14T17:40:25+05:30

783 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements