×

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code.

The Ganesha will be printed in the following manner using the "for" loop

Program for the Ganesha’s Pattern

import java.io.*;
import java.util.*;
public class GaneshaPattern
{  
    // start of the main() method 
    public static void main(String[] args)   
    {  
        // for the printing of Ganesha's crown


        // for printing the first part of the crown 
for(int j = 1; j <= 3; j++)  
        {  
for(int k = 1; k <= 11-j+1; k++)  
            {   
System.out.print(" ");  //  for printing space  
            }  
for(int k = 1; k <= ((j*j)+2-j)/2; k++)  
            {  
System.out.print("*");  // for printing star  
            }  
System.out.println();  
        }  
        //for printing the second part of the crown 
for(int j = 1; j <= 3; j++)  
        {  
for(int k = 1; k <= 2*(4-j+1); k++)  
            {  
System.out.print(" ");  
            }  
for(int k = 1; k <= 2+(4*j); k++)  
            {  
System.out.print("*");  
            }  
System.out.println();  
        }  
System.out.println();  


        // for loop for the Ganesh trunk design
for(int i = 1; i<= 2; i++)  
        {  
for(int u = 1; u <= 2-i+1; u++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int u = 1; u <= i*(i*i)-(i*i); u++)  
            {  
System.out.print(" ");  
            }  
for(int u = 1; u <= 2*(2-i+1)-1; u++)  
            {  
System.out.print("*");  
            }  
for(int u = 1; u <= 2*(5-i+1); u++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int u = 1; u <= i*(i*i)-(i*i); u++)  
            {  
System.out.print(" ");  
            }  
for(int u = 1; u <= 2*(2-i+1)-1; u++)  
            {  
System.out.print("*");  
            }  
System.out.println();  
        }  


for(int u = 1; u <= 3; u++)  
        {  
for(int v = 1; v <= u; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= (11-u+1)/2; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= 5-u+1; v++)  
            {  
System.out.print(" ");  
            }  
for(int v = 1; v <= 1-u+1; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= 5; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
System.out.println();  
        }  


for(int u = 1; u <= 4; u++)  
        {  
for(int v = 1; v <= 8; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= 3; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
System.out.println();  
        }  


for(int u = 1; u <= 4; u++)  
        {  
for(int v = 1; v <= 8+u; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= (7-u+1)/2; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
System.out.println();  
        }  
for(int u = 1; u <= 2; u++)  
        {  
for(int v = 1; v <= 12; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= 2-u+1; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
System.out.println();  
        }  


for(int u = 1; u <= 5; u++)  
        {  
for(int v = 1; v <= 11-u+1; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= 1; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
System.out.println();  
        }  


for(int u = 1; u <= 4; u++)  
        {  
for(int v = 1; v <= 6+u; v++)  
            {  
System.out.print(" ");  
            }  
System.out.print("*");  
for(int v = 1; v <= (3-u+1)/2; v++)  
            {  
System.out.print(" ");  
System.out.print("*");  
            }  
System.out.println();  
        }  


System.out.println();  


for(int u = 1; u <= 2; u++)  
        {  
for(int v = 1; v <= 4+(u*2); v++)  
            {  
System.out.print(" ");  
            }      
for(int v = 1; v <= 4*(2-u+1)+1; v++)  
            {  
System.out.print("*");  
            }  
System.out.println();  
        }  


for(int u = 1; u <= 1; u++)  
        {  
for(int v = 1; v <= 10; v++)  
            {  
System.out.print(" ");              
            }  
System.out.print("*");  
        }  
    }  
} 

The output of the above program

Ganesha’s Pattern in Java

Related Topics

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Java Math IEEEremainder() Method

The IEEEremainder() method of Math class calculates the remainder as prescribed by the IEEE754 standard. This method simply returns the remainder when f1 (dividend) is divided by f2 (divisor). Syntax: public static...

2 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Java BufferedWriter

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Vigesimal in Java

A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty...

4 minutes read.