
Write a Java Program to design a Swastik of given size.
In this program, we will learn about how to write the code for Swastik Pattern in Java by using stars, with the user-defined rows and columns.
To get the perfect Swastik shape, the user should input odd numbers for rows and columns so as to find the mid of rows and columns easily.
* * * * *
* *
* *
* * * * * * *
* *
* *
* * * * *
Program:
import java.util.Scanner;
class swastik
{
public static void main(String args[])
{
int n,row, col;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size greater than 4 to get the perfect shape of the Swastik... ");
n=sc.nextInt();
if (n%2==0)
row=n;
col=n;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (i < (row / 2) )
{
if (j < (col / 2) )
{
if (j == 0)
System.out.print("*");
else
System.out.print(" "+ " ");
}
else if (j == (col / 2))
System.out.print(" *");
else
{
if (i == 0)
System.out.print(" *");
}
}
else if (i == (row / 2) )
System.out.print("* ");
else
{
if (j == (col / 2) || j == col - 1)
System.out.print("* ");
else if (i == row - 1)
{
if (j <= (col / 2) || j == col - 1)
System.out.print("* ");
else
System.out.print(" "+ " ");
}
else
System.out.print(" "+" ");
}
}
System.out.print("\n");
}
}
}
/*
Output:
Enter the number greater than 4 to
get the perfect shape of the Swastik
7
* *
* *
* * * * * * *
* *
* *
* * * * *
*/
Read also :