×

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We can use java, C++, C, python and other programming languages also. We can develop our coding skills and logical thinking by solving the patterns. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic. In these loops, the first loop work for rows and the second loop work for columns. The time and space complexity of the program pattern depends on the number of loops and statements we use and a number of variables we use in the program we have written.

Here, we are going to learn about how to print the crown pattern by using java programming language using loops. We can print crown patterns by using stars, hash, also by using numbers. Like this, we can print the pattern of the crown by using any symbol we want, not only a crown but also other patterns too.

How do we print the crown pattern?

Crown Pattern in Java

To print the crown pattern, we divide the whole crown into two parts. The reason behind dividing it into parts is to write the code easily and to print the pattern efficiently. If we observe the above picture, we divide the first part into five rows. And the second pattern into two rows.

Approach to follow to print the crown pattern.

  • Either we can mention the number of rows, or else we can read the number of rows from the keyboard, after reading store the number of values in an integer.
  • By using (rows-1)/2, we can calculate the height of the crown pattern.
  • We have to define two loops for printing the pattern.
    The first for loop is an outer loop and the second for loop is the inner loop.
  • For printing the symbol, we can use the first for loop.
  • For printing the row value also, we can make the first for loop.
  • Use the below condition for printing the pattern,
    if (column == 0) ||column == height||column == row – 1) and (r == height – 1) and
    if (column < r||column > height – r) && (column < height + row|| column >= row – r))
    else print the spaces in the pattern.
  • Now print the symbol that you want according to the loop.

Example 1: Crown Pattern

CrownPatternExampleOne.java

public class CrownPatternExampleOne  
{  
  public static void main(String[]args)  
  {   
     for(int i=1; i<=5; i++)  
     {  
         for(int k=1; k<=i; k++)  
         {  
             System.out.print(" ");  
         }   
         for(int j=1; j<=i; j++)  
         {  
             System.out.print("#");  
         }   
         for(int k=1; k<=3*(4-i+1); k++)  
         {  
             System.out.print(" ");  
         }   
         for(int j=1; j<=2*i-1; j++)  
         {  
             System.out.print("#");  
         }   
         for(int k=1; k<=3*(4-i+1); k++)  
         {  
             System.out.print(" ");  
         }   
         for(int j=1; j<=i; j++)  
         {  
             System.out.print("#");  
         }   
         System.out.println();  
     }  
     for(int i=1; i<=2; i++)  
     {  
         for(int k=1; k<=5+i-1; k++)  
         {  
             System.out.print(" ");  
         }   
         for(int j=1; j<=2*(9-i+1)+1; j++)  
         {  
             System.out.print("#");  
         }   
         System.out.println();  
     }  
  }   
}  
Crown Pattern in Java

Example 2: Crown Pattern

CrownPatternExampleTwo.java

import java.io.*;
 
public class CrownPatternExampleTwo
{
    static void crown(int length, int height)
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < length; j++)
            {
                if (i == 0)
                {
                    if (j == 0 || j == height || j == length - 1)
                    {
                        System.out.print("#");
                    }
                    else {
                        System.out.print(" ");
                    }
                }
                else if (i == height - 1)
                {
                    System.out.print("#");
                }
                else if ((j < i || j > height - i) &&
                        (j < height + i || j >= length - i))
                    System.out.print("#");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
    public static void main (String[] args)
    {
        int length = 30;
        int height = (length - 1) / 2;
        crown(length, height);
                 
    }
}
Crown Pattern in Java

Example 3: Crown Pattern

In this crown pattern program, we can choose the number of rows we want to print.

Let us see the program.

CrownPatternExampleThree.java

import java.util.*;
public class CrownPatternExampleThree
{    
    public static void main(String args[])   
    {   
    int row,r,c,h;
    char crown;
    Scanner s = new Scanner(System.in);
    System.out.print("Enter the number of rows : ");
    row = s.nextInt();
    System.out.print("Enter character you want print the crown pattern: ");
    crown = s.next().charAt(0);
    h= (row-1)/2;
    for (r = 0; r < h ; r++)
        {
            for (c = 0; c < row; c++)
            {
                if (r == 0)
                {
                    if (c == 0 || c  == h  || c == row - 1)
                        System.out.print(crown);
                    else
                        System.out.print(" ");
                }
                else if (r == h- 1)
                    System.out.print(crown);
                else if ((c < r || c > h  - r) &&(c < h  + r || c >= row - r))
                    System.out.print(crown);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
  }
}
Crown Pattern in Java

Related Topics

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

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

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

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