Signup/Sign In

Java Program to Print the Left Arrow Star Pattern

In this tutorial, we will see how to print the left arrow star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the pattern. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.

Input: Enter the number of rows: 6

Output: The pattern is:

******

*****

****

***

**

*

**

***

****

*****

******

Program 1: Print the Left Arrow Star Pattern

In this program, we will see how to print the left arrow star pattern in Java using a for loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use two for loops to print the pattern.
  6. The first for loop displays the upper pattern of “left arrow” and the second for loop displays the lower pattern.
  7. First, check the condition i<n at for loop, if it is true, then it executes the first inner for the loop until the condition is false, after that, it executes the second inner for the loop until the condition is false.
  8. The first inner for loop display space and the second inner for loop displays character which we have given to display.
  9. After the execution of the first outer for loop, the second outer for loop will be executed.
  10. Check the condition at for loop, if it is true, then execute the inner loops until the condition i<n.
  11. Display the result.
  12. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Left Arrow Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        for(int i=1;i<=n;i++)
        {
	        for(int j=1;j<=n-i;j++)
            { 
                System.out.print(" ");
            }
		    for(int j=i;j<=n;j++)
            {
                System.out.print("*");
            }
	        System.out.println();
        }            
        for(int i=1;i<n;i++)
        {
	        for(int j=0;j<i;j++)
            {
                System.out.print(" ");
            }
		    for(int j=0;j<=i;j++)
            {
               System.out.print("*");
            } 
	        System.out.println();
        }    
    }
}


Enter the number of rows: 6
******
*****
****
***
**
*
**
***
****
*****
******

Program 2: Print the Left Arrow Star Pattern

In this program, we will see how to print the left arrow star pattern in Java using a while loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use two while loops to print the pattern.
  6. First, check the condition i<=n at while, if it is true then it executes the code in the while loop.
  7. The first while will execute until i<=n is false.
  8. After the execution of the first while loop, the second while loop will be executed.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Left Arrow Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        int i=1;
        int j;
	    while(i<=n)
        {
	        j=1;
		    while(j<=n-i)
            { 
                System.out.print(" ");
                j++;
           }
		    j=i;
            while(j<=n)  
            {
                System.out.print("*");
		        j++;
            }
	        System.out.println();
	        i++;
        }            
        i=1;
	    while(i<n)
        {
	        j=0;
		    while(j<i)
            {
                System.out.print(" ");
		        j++;
            }
		    j=0;
	        while(j<=i)
            {
                System.out.print("*");
		        j++;
            } 
	        System.out.println();
	        i++;
        }    
    }
}


Enter the number of rows: 6
******
*****
****
***
**
*
**
***
****
*****
******

Program 3: Print the Left Arrow Star Pattern

In this program, we will see how to print the left arrow star pattern in Java using a do-while loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use two do-while loops to print the pattern.
  6. At first, the do-while loop will be executed until the condition is false i<=n. Inner do-while loops will be executed until the condition is false.
  7. After the execution of the first do-while loop, the second do-while loop will be executed until the condition i<n is false. Inner do-while loops will be executed until the condition is false.
  8. Display the result.
  9. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Left Arrow Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        int i=1;
        int j;
	    do
        {
	        j=1;
            do
            { 
                System.out.print(" ");
            }while(j++<=n-i);
		    j=i;
            do
            {
                System.out.print("*");
		        j++;
            }while(j<=n); 
	        System.out.println();
	        i++;
        } while(i<=n);           
        i=1;
	    do
        {
	        j=0;
            do
            {
                System.out.print(" ");
            }while(j++<i);
		    j=0;
		    do
		    {
                System.out.print("*");
		        j++;
            } while(j<=i);
	        System.out.println();
	        i++;
        }while(i<n);  
    }
}


Enter the number of rows: 6
******
*****
****
***
**
*
**
***
****
*****
******



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.