Java program to print plus star pattern program – We have written the below print/draw Plus 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 Plus Star Pattern – Using For Loop
- Print – Using While Loop
- Print – Using Do While Loop
Using For Loop
1) First checks the condition( i<=n*2-1)at the outer for loop, if it is true then it comes into the loop and checks the
“if “condition. If the “if” condition is true, then the first inner “for” loop will be executed, otherwise “else” part will be executed i.e 2nd inner “for” loop.
2) In 1st inner loop, if the condition true then checks the “if ” condition, if “if” condition is true then it displays the character which we have given to display and displays space,the code in the 1st inner “for” loop will execute until the inner for loop condition is false, then the cursor comes to outer “for” loop, if condition is true again, the same process will continue.
3) The 2nd inner for loop will execute only the if condition at the outer “for” loop is false, in this for loop if the condition is true at “for” loop then it 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 |
import java.util.Scanner; public class Plus { 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*2-1;i++) { if(i!=n) for(int j=1;j<=n;j++) { if(j==n) System.out.print(c); System.out.print(" "); } else for(int j=1;j<=n*2-1;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 at while loop, if it is true then it checks the “if “condition, if the “if” condition is true then it executes first inner while loop otherwise executes else part i.e 2nd inner while loop.
2) The code will execute until the condition at while loop is false i.e 1<=n*2-1
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 |
import java.util.Scanner; public class Plus { 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*2-1) { if(i!=n) { j=1; while(j<=n) { if(j==n) System.out.print(c); System.out.print(" "); j++ ; } } else { j=1; while(j<=n*2-1) { 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) Do- while loop executes the code one time then checks the condition.
2) The first outer do-while loop will execute, then checks the condition i<=n*2-1.
3) If the ” if condition i!=n” is true, then 1st inner do loop will be executed otherwise 2nd inner do loop will be executed.
4) The total code in outer do-while loop executes until the condition false i.e i<=n*2-1.
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 Plus { 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 { if(i!=n) { j=1; do { if(j==n) System.out.print(c); System.out.print(" "); j++ ; }while(j<=n); } else { j=1; do { System.out.print(c); j++; }while(j<=n*2-1); } System.out.println(); i++; } while(i<=n*2-1); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Enter N : 7 Enter Symbol : * * * * * * * ************* * * * * * * |
More Programs: