×

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is very difficult to code.We need to make certain that the user enters odd numbers for the rows and columns to obtain the ideal Swastika since it will be simple to locate the middle of the rows and columns.

The rows and columns must be the same size and have an odd number.The result will be a flawless swastika pattern.

The following is how the Swastika will be printed using the "for" loop

Program for the Swastika Pattern in Java

import java.io.*;
import java.util.Scanner;   
class SwastikaPattern
{  
    public static void main (String[] args)  
    {  
        int rows, cols;  
        Scanner sc = new Scanner(System.in);  
System.out.println("Please enter odd numbers for rows and columns to get perfect Swastika.");  
System.out.println("Enter total rows");  
        rows = sc.nextInt();  
System.out.println("Enter total colums");  
        cols = sc.nextInt();  
sc.close();  
designSwastika(rows, cols);  
    }  
    static void designSwastika(int rows, int cols)  
    {  
        for (int u = 0; u < rows; u++) 
{  
            for (int v = 0; v < cols; v++)  
            {  
                //check to see if u is less than half of the rows or not.  
                if (u < rows / 2)  
                {  
                    // check to see if v is less than half of the cols or not  
                    if (v < cols / 2)  
                    {  
                        // if v == 0, print "*"
                        if (v == 0)   
                        {  
System.out.print("*");  
                        }  
                        // if not, we print space
                        else   
                        {  
System.out.print(" "+ " ");  
                        }  
                    }  
                    // check to see if v is equal to half of the columns. 
                    else if (v == cols / 2)   
                    {  
System.out.print(" *");  
                    }  
                    else  
                    {  
                        // The first row will have a "*" if u = 0.
                        if (u == 0)  
System.out.print(" *");  
                    }  
                }  
                // check to see if u equal half of the rows or not.
                else if (u == rows / 2) {  
System.out.print("* ");  
                }  
                else  
                {  
                    // The middle and final columns will have the symbol "*" when u > rows/2.
                    if (v == cols / 2 || v == cols - 1)  
                    {  
System.out.print("* ");  
                    }  
                    else if (u == rows - 1)  
                    {  
                        // The very last row will have a '*' if v = cols/2 or  if it is the last column.
                        if (v <= cols / 2 || v == cols - 1)   
                        {  
System.out.print("* ");  
                        }  
                        else {  
System.out.print(" "+ " ");  
                        }  
                    }  
                    else  
System.out.print(" "+" ");  
                }  
            }  
System.out.print("\n");  
        }  
    }  
}

Output of the above program

Swastika Pattern in Java

Related Topics

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.