Java Program to Print the Right Arrow Star Pattern
In this tutorial, we will see how to print the right arrow star pattern in java First, we will ask the user to initialize the number of rows. Then, we will use different loops to print the right arrow star 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:
******
*****
****
***
**
*
**
***
****
*****
******
This can be done by using the following methods:
Approach 1: Using a For Loop
Approach 2: Using a While loop
Approach 3: Using a do-while loop
Let us look at each of these approaches for a better understanding.
Program 1: Java Program to Print the Right Arrow Star Pattern
In this program, we will see how to print the right arrow star pattern in java using a for loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use two for loops to print the pattern.
-
Use the outer for loop for the rows.
-
Use the inner for loop for the columns.
-
The first outer for loop displays the half of the pattern and the second outer for loop displays the next half of the pattern.
-
In the first outer loop, the condition will be checked if it is true then it checks the inner for loop condition if it is true then checks the ” if” condition. ” if” condition true then displays space otherwise displays characters which we have given to display.
-
The inner for loop executes the code until the condition is false. The 1st outer loop will display this pattern.
-
Display the pattern.
-
Stop.
Let us look at the below example to understand the implementation of the above algorithm.
//Java Program to Print the Right 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=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(j<i)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
for(int i=2;i<=n;i++)
{
for(int j=0;j<n;j++)
{
if(j<n-i)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
}
Enter the number of rows: 5
*****
****
***
**
*
**
***
****
*****
Program 2: Java Program to Print the Right Arrow Star Pattern
In this program, we will see how to print the right arrow star pattern in java using a while loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use two while loops to print the pattern.
-
While loop first checks if the condition is true or not, if true then it executes the code.
-
The first outer while loop will display half of the pattern and the second outer loop will display the next half of the pattern.
-
The condition at first outer while loop is true, then it comes to the inner loop, the inner loop condition also true then checks the if condition, “if” condition is true, then it displays space otherwise will display character”*”.The inner loop will execute the code until the condition is false. The 1st outer loop executes the code until the condition i<n.
-
The cursor comes to the next line then the second outer while loop will be executed until the condition is false.
-
Display the result.
-
Stop.
Let us look at the below example to understand the implementation of the above algorithm.
//Java Program to Print the Right 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=0;
int j;
while(i<n)
{
j=0;
while(j<n)
{
if(j<i)
System.out.print(" ");
else
System.out.print("*");
j++;
}
System.out.println();
i++;
}
i=2;
while(i<=n)
{
j=0;
while(j<n)
{
if(j<n-i)
System.out.print(" ");
else
System.out.print("*");
j++;
}
System.out.println();
i++;
}
}
}
Enter the number of rows: 5
*****
****
***
**
*
**
***
****
*****
Program 3: Java Program to Print the Right Arrow Star Pattern
In this program, we will see how to print the right arrow star pattern in java using a do-while loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use two do-while loops to print the pattern.
-
In the first outer do-while loop, it executes the code and then checks the condition i<n. The 1st outer do-while loop will execute the code until the condition i<n false. It will display the first half of the pattern.
-
The second outer do-while loop will execute the code until the condition i<n is false. It will display the second half of the pattern.
-
Display the result.
-
Stop
Let us look at the below example to understand the implementation of the above algorithm.
//Java Program to Print the Right 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=0;
int j;
do
{
j=0;
do
{
if(j<i)
System.out.print(" ");
else
System.out.print("*");
j++;
}while(j<n);
System.out.println();
i++;
}while(i<n);
i=2;
do
{
j=0;
do
{
if(j<n-i)
System.out.print(" ");
else
System.out.print("*");
j++;
}while(j<n);
System.out.println();
i++;
}while(i<=n);
}
}
Enter the number of rows: 8
********
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
********