×

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

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute 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.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

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.

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Java exception list

Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular...

6 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

Streams in Java

The conventional Java SE 8 version came with many new peculiarities, out of which the most striking of which are assuredly lambda expressions and the method references. Streams and Streams...

14 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.