×

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

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.