Program to print Crown Pattern

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to print
// Crown pattern
#include <bits/stdc++.h>
using namespace std;
  
// function to print crown pattern
void crown(int length, int height)
{
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < length; j++) {
  
            // for first row, print '*'
            // i.e, for top part of crown
            if (i == 0) {
  
                // print '*' at first, middle and last column
                if (j == 0 || j == height || j == length - 1) {
                    cout << "*";
                }
                else {
                    cout << " ";
                }
            }
  
            // print '*' at base of
            // crown i.e, for last row
            else if (i == height - 1) {
                cout << "*";
            }
  
            // fill '#' to make a perfect crown
            else if ((j < i || j > height - i) &&
                     (j < height + i || j >= length - i))
                cout << "#";
            else
                cout << " ";
        }
        cout << "\n";
    }
}
  
// Driver code
int main()
{
    // length of crown
    int length = 25;
  
    // height of crown
    int height = (length - 1) / 2;
  
    // function calling
    crown(length, height);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to print
// Crown pattern
import java.io.*;
  
class GFG 
{
    // function to print crown pattern
    static void crown(int length, int height)
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < length; j++) 
            {
      
                // for first row, print '*'
                // i.e, for top part of crown
                if (i == 0
                {
      
                    // print '*' at first, middle and last column
                    if (j == 0 || j == height || j == length - 1
                    {
                        System.out.print("*");
                    }
                    else {
                        System.out.print(" ");
                    }
                }
      
                // print '*' at base of
                // crown i.e, for last row
                else if (i == height - 1
                {
                    System.out.print("*");
                }
      
                // fill '#' to make a perfect crown
                else if ((j < i || j > height - i) &&
                        (j < height + i || j >= length - i))
                    System.out.print("#");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        // length of crown
        int length = 25;
          
        // height of crown
        int height = (length - 1) / 2;
          
        // function calling
        crown(length, height);
                  
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation to 
# print Crown pattern
  
# Function to print
# crown pattern
def crown(length, height) :
  
    for i in range(0, height) :
        for j in range(0, length) :
  
            # for first row, print '*'
            # i.e, for top part of crown
            if (i == 0) :
  
                # print '*' at first, 
                # middle and last column
                if (j == 0 or j == height or 
                              j == length - 1) :
                    print ("*", end = "")
  
                else :
                    print (" ", end = "")
  
            # print '*' at base of
            # crown i.e, for last row
            elif (i == height - 1) :
                print ("*", end = "")
              
  
            # fill '#' to make 
            # a perfect crown
            elif ((j < i or j > height - i) and 
                  (j < height + i or 
                   j >= length - i)) : 
                print ("*", end = "")
  
            else :
                print (" ", end = "")
          
        print () 
  
  
# Driver code
  
# length of crown
length = 25
  
# height of crown
height = int((length - 1) / 2)
  
# function calling
crown(length, height)
  
# This code is contributed by 
# Manish Shaw(manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to print
// Crown pattern
using System;
  
class GFG {
      
    // function to print crown pattern
    static void crown(int length, int height)
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < length; j++) 
            {
      
                // for first row, print '*'
                // i.e, for top part of crown
                if (i == 0) 
                {
      
                    // print '*' at first, middle
                    // and last column
                    if (j == 0 || j == height 
                             || j == length - 1) 
                    {
                        Console.Write("*");
                    }
                    else {
                        Console.Write(" ");
                    }
                }
      
                // print '*' at base of
                // crown i.e, for last row
                else if (i == height - 1) 
                {
                    Console.Write("*");
                }
      
                // fill '#' to make a perfect crown
                else if ((j < i || j > height - i) &&
                                 (j < height + i ||
                                  j >= length - i))
                    Console.Write("#");
                else
                    Console.Write(" ");
            }
              
            Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main () 
    {
        // length of crown
        int length = 25;
          
        // height of crown
        int height = (length - 1) / 2;
          
        // function calling
        crown(length, height);
                  
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to print
// Crown pattern
  
// Function to print crown pattern
function crown($length,$height)
{
    for ($i = 0; $i < $height; $i++) 
    {
        for ($j = 0; $j < $length; $j++) 
        {
  
            // for first row, print '*'
            // i.e, for top part of crown
            if ($i == 0) 
            {
  
                // print '*' at first, 
                // middle and last column
                if ($j == 0 || 
                    $j == $height || 
                    $j == $length - 1) 
                {
                    echo "*";
                }
                else 
                {
                    echo " ";
                }
            }
  
            // print '*' at base of
            // crown i.e, for last row
            else if ($i == $height - 1) 
            {
                echo "*";
            }
  
            // fill '#' to make a perfect crown
            else if (($j < $i || 
                      $j > $height - $i) &&
                     ($j < $height + $i || 
                      $j >= $length - $i))
  
                echo "#";
            else
                echo " ";
        }
        echo "\n";
    }
}
  
// Driver code
  
// length of crown
$length = 25;
  
// height of crown
$height = ($length - 1) / 2;
  
// function calling
crown($length, $height);
  
// This code is contributed by mits.
?>

chevron_right


Output:

*           *           *
#           #           #
##         ###         ##
###       #####       ###
####     #######     ####
#####   #########   #####
###### ########### ######
#########################
#########################
#########################
#########################
*************************


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar, manishshaw1