Java program to print Left Arrow star pattern program. We have written below the print/draw Left Arrow asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we have added compiler so that you can execute the below codes – List of Complete Pattern Programs in Java here.
- Print Left Arrow Star Pattern – Using For Loop
- Print – Using While Loop
- Print – Using Do While Loop
Using For Loop
1) First, for loop displays, the upper pattern of “left arrow” and 2nd for loop displays the lower pattern.
2) First checks 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 2nd inner for the loop until the condition is false.
3) The 1st inner for loop display space and the 2nd inner for loop displays character which we have given to display.
4) After execution of 1st outer for loop, the 2nd outer for loop will be executed.
5) Checks the condition at for loop, if it is true, then executes the inner loops until the condition i<n.
1st inner loop displays the space and 2nd inner loop displays the character which we have given to display.
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 63 64 65 66 |
import java.util.Scanner; public class Larrow { 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=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(c); } 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(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) First checks the condition i<=n at while, if it is true then it executes the code in while loop.
2) First, while will executes until i<=n is false.
3) After execution of first while loop 2nd while loop will be executed.
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 63 64 65 66 67 68 |
import java.util.Scanner; public class Larrow { 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=1; int j; while(i<=n) { j=1; while(j<=n-i) { System.out.print(" "); j++; } j=i; while(j<=n) { System.out.print(c); 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(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) 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.
2) After execution of the first do-while loop, the 2nd 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.
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 63 64 65 66 67 68 69 70 71 72 73 |
import java.util.Scanner; public class Larrow { 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=1; int j; do { j=1; do { System.out.print(" "); }while(j++<=n-i); j=i; do { System.out.print(c); 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(c); j++; } while(j<=i); System.out.println(); i++; }while(i<n); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter N : 5 Enter Symbol : . ..... .... ... .. . .. ... .... ..... |
More Programs: