Java Program to Print Swastika Sign By Taking User Input
Take as input N, an odd number (>=5). Print the following pattern as given below for N = 7. Here the input format is the value of N and output results out in swastika pattern printing.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input Format: Enter value of N ( >=5 ) Constraints: 5 <= N <= 99
Output Format: Print the required pattern.
* * * * * * * * * * * * * * * * * * * * * * * * *
Let us thinks of a way out in order to figure out by listing the logic prior to writing any code in order to generate the desired pattern.
- First, take an input (N) from the user.
- Initialize rows and cols as 1 to make loops in for the statement.
- In the first row, we definitely know we have to print a * then give some spaces till N/2, and then again print stars till N.
- In the second to N/2 rows, first, we need to print a * then spaces till N/2 (that it , we don’t need to provide spaces here)
- In the (N/2)+1 th row, just print * till N.
- Again in (N/2) +2th row to N -1, we need to first print spaces till N/2 and then print a * and the spaces till N-1 and finally a * at the end
- In the final row, we need to print * till (N/2) + 1 and then print spaces till N-1 now and then finally ending it a *.
- Lastly do not forget to add System.out.println() to add a blank line after each line of work
Now let us implement the some by writing the core for the same.
Note: One can also use BufferedReader class if you want to take input from user to enter the number of rows here.