Java Program to Print Crown Pattern
In this tutorial, we are going to print crown pattern. Print the crown pattern using the characters " * " and " & " based on the length value.
Example:
Input: length = 28
Output:
* * *
# # #
# # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
* * * * * * * * * * * * * * * * * * * * * * * * * * *
Explanation: We print the crown using “ * ” and “ # ” by the given input length.
Approach: Using procedural pattern printing algorithm
ALGORITHM:
Step 1: Determine the number of rows by height.
Step 2: Determine the number of columns by the length of the crown.
Step 3: Repeat the process for every row and column in the pattern.
Step 4: Use the position of each character in the grid in relation to the structure of the crown to determine which character to print at that position.
step 5: Create the contour of the top of the crown.
Step 6: Print '*' at the beginning, middle, and end places in the first row (i.e., the top portion of the crown).
Step 7: Use blank characters to fill in the gaps.
Step 8: Print '*' across all columns in the bottom row to form the base of the crown.
Step 9: Use the '#' characters to fill the remaining rows that make up the centre portion of the crown.
Step 10: Arrange '*' and '#' characters so that the familiar shape of a crown appears inside the grid.
Filename: PrintCrownPattern.java
import java.io.*;
public class PrintCrownPattern
{
// Print the crown pattern
static void crownPrinting(int len, int hgt)
{
// len and hgt stands for length and height of the crown
for (int a = 0; a < hgt; a++)
{
for (int b = 0; b < len; b++)
{
// Print '*' for first row,
// that is, for top part of the crown
if (a == 0)
{
// Print '*' at first, middle and last column
if (b == 0 || b == hgt || b == len - 1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
// At base of the crown print '*'
// that is, for last row
else if (a == hgt - 1)
{
System.out.print("*");
}
// To make a perfect crown fill '#'
else if ((b < a || b > hgt - a) && (b < hgt + a || b >= len - a))
System.out.print("#");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main (String[] args)
{
// length of the crown is
int len = 27;
// height of the crown is
int hgt = (len - 1) / 2;
crownPrinting(len, hgt);
}
}
Output:
* * *
# # #
## ### ##
### ##### ###
#### ####### ####
##### ######### #####
###### ########### ######
######## ############ #######
###########################
###########################
###########################
###########################
***************************
Complexity analysis:
Time complexity: The program has the time complexity of O(l2).
Space complexity: The program has the space complexity of O(1).