×

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

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

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.

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

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.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 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 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.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.