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++
// 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; } |
Java
// 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. |
Python3
# 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) |
C#
// 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. |
PHP
<?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. ?> |
Javascript
<script> // JavaScript implementation to print // Crown pattern // function to print crown pattern function crown(length, height) { for ( var i = 0; i < height; i++) { for ( var 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) { document.write( "*" ); } else { document.write( " " ); } } // print '*' at base of // crown i.e, for last row else if (i == height - 1) { document.write( "*" ); } // fill '#' to make a perfect crown else if ( (j < i || j > height - i) && (j < height + i || j >= length - i) ) document.write( "#" ); else document.write( " " ); } document.write( "<br>" ); } } // Driver code // length of crown var length = 25; // height of crown var height = parseInt((length - 1) / 2); // function calling crown(length, height); </script> |
* * * # # # ## ### ## ### ##### ### #### ####### #### ##### ######### ##### ###### ########### ###### ######################### ######################### ######################### ######################### *************************
Time complexity: O(l2) for given length
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!