Java program to print Right Arrow star pattern program. We have written the below print/draw right arrow asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we added compiler such that you can execute the below codes.
- Print Right Arrow Star Pattern – Using For Loop
- Print – Using While Loop
- Print – Using Do While Loop
Using For Loop
1) Outer loop for rows and inner loops for columns.
2) The 1st outer for loop displays the half of the pattern and 2nd outer for loop displays the next half of the pattern.
3) In 1st 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 character which we have given to display.
4) The inner for loop executes the code until the condition is false. The 1st outer loop will display this pattern.
1 2 3 4 5 |
..... .... ... .. . |
5) After that cursor comes to next line and the 2nd outer loop will be executed.
6) The condition at 2nd outer loop is true, then the inner loop will be executed until the condition false.
In inner loop if “if” condition is true, then it displays space otherwise it displays character which we have given to display.
The code in the Outer loop will be executed until the condition is false. The 2nd outer for loop will display this pattern.
1 2 3 4 |
.. ... .... ..... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.Scanner; public class Rarrow { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(j<i) System.out.print(" "); else System.out.print(c); } 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(c); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter N : 5 Enter Symbol : . ..... .... ... .. . .. ... .... ..... |
Using While Loop
1) While loop first checks if the condition is true or not, if true then it executes the code.
2) The 1st outer while loop will display half of the pattern and the 2nd outer loop will display next half of the pattern.
3) 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 condition false. The 1st outer loop executes the code until the condition i<n.
4) Cursor comes to next line then 2nd outer while loop will be executed until the condition false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import java.util.Scanner; public class Rarrow { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); int i=0; int j; while(i<n) { j=0; while(j<n) { if(j<i) System.out.print(" "); else System.out.print(c); 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(c); j++; } System.out.println(); i++; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter N : 5 Enter Symbol : * ***** **** *** ** * ** *** **** ***** |
Using Do-While Loop
1) In the 1st 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.
2) The 2nd outer do-while loop will execute the code until the condition i<n is false. It will display the second half of the pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import java.util.Scanner; public class Rarrow { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); int i=0; int j; do { j=0; do { if(j<i) System.out.print(" "); else System.out.print(c); 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(c); j++; }while(j<n); System.out.println(); i++; }while(i<=n); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Enter N : 10 Enter Symbol : ~ ~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~ ~~~~~~~ ~~~~~~ ~~~~~ ~~~~ ~~~ ~~ ~ ~~ ~~~ ~~~~ ~~~~~ ~~~~~~ ~~~~~~~ ~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~ |
More Programs: