Program to print Crown Pattern
Last Updated :
20 Feb, 2023
Given the value of length, print the crown pattern using ‘*’ and ‘#’.
Note : In order to print a perfect crown, take the value of length as an odd number greater than 15.
Examples :
Input: length = 19
Output:
* * *
# # #
## ### ##
### ##### ###
#### ####### ####
###################
###################
###################
*******************
Input: length = 25
Output:
* * *
# # #
## ### ##
### ##### ###
#### ####### ####
##### ######### #####
###### ########### ######
#########################
#########################
#########################
#########################
*************************
Below is the implementation to print crown pattern :
C++
#include <bits/stdc++.h>
using namespace std;
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) {
cout << "*" ;
}
else {
cout << " " ;
}
}
else if (i == height - 1) {
cout << "*" ;
}
else if ((j < i || j > height - i) &&
(j < height + i || j >= length - i))
cout << "#" ;
else
cout << " " ;
}
cout << "\n" ;
}
}
int main()
{
int length = 25;
int height = (length - 1) / 2;
crown(length, height);
return 0;
}
|
Java
import java.io.*;
class GFG
{
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 = 25 ;
int height = (length - 1 ) / 2 ;
crown(length, height);
}
}
|
Python3
def crown(length, height) :
for i in range ( 0 , height) :
for j in range ( 0 , length) :
if (i = = 0 ) :
if (j = = 0 or j = = height or
j = = length - 1 ) :
print ( "*" , end = "")
else :
print ( " " , end = "")
elif (i = = height - 1 ) :
print ( "*" , end = "")
elif ((j < i or j > height - i) and
(j < height + i or
j > = length - i)) :
print ( "*" , end = "")
else :
print ( " " , end = "")
print ()
length = 25
height = int ((length - 1 ) / 2 )
crown(length, height)
|
C#
using System;
class GFG {
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)
{
Console.Write( "*" );
}
else {
Console.Write( " " );
}
}
else if (i == height - 1)
{
Console.Write( "*" );
}
else if ((j < i || j > height - i) &&
(j < height + i ||
j >= length - i))
Console.Write( "#" );
else
Console.Write( " " );
}
Console.WriteLine();
}
}
public static void Main ()
{
int length = 25;
int height = (length - 1) / 2;
crown(length, height);
}
}
|
PHP
<?php
function crown( $length , $height )
{
for ( $i = 0; $i < $height ; $i ++)
{
for ( $j = 0; $j < $length ; $j ++)
{
if ( $i == 0)
{
if ( $j == 0 ||
$j == $height ||
$j == $length - 1)
{
echo "*" ;
}
else
{
echo " " ;
}
}
else if ( $i == $height - 1)
{
echo "*" ;
}
else if (( $j < $i ||
$j > $height - $i ) &&
( $j < $height + $i ||
$j >= $length - $i ))
echo "#" ;
else
echo " " ;
}
echo "\n" ;
}
}
$length = 25;
$height = ( $length - 1) / 2;
crown( $length , $height );
?>
|
Javascript
<script>
function crown(length, height) {
for ( var i = 0; i < height; i++) {
for ( var j = 0; j < length; j++) {
if (i == 0) {
if (j == 0 || j == height || j == length - 1) {
document.write( "*" );
} else {
document.write( " " );
}
}
else if (i == height - 1) {
document.write( "*" );
}
else if (
(j < i || j > height - i) &&
(j < height + i || j >= length - i)
)
document.write( "#" );
else document.write( " " );
}
document.write( "<br>" );
}
}
var length = 25;
var height = parseInt((length - 1) / 2);
crown(length, height);
</script>
|
Output:
* * *
# # #
## ### ##
### ##### ###
#### ####### ####
##### ######### #####
###### ########### ######
#########################
#########################
#########################
#########################
*************************
Time complexity: O(l2) for given length
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...