Given the value of n, i.e, number of lines, print the pattern.
Examples :
Input: n = 5
Output:
*_*****_*****_*
**_****_****_**
***_***_***_***
****_**_**_****
*****_*_*_*****Input: n = 10
Output:
*_**********_**********_*
**_*********_*********_**
***_********_********_***
****_*******_*******_****
*****_******_******_*****
******_*****_*****_******
*******_****_****_*******
********_***_***_********
*********_**_**_*********
**********_*_*_**********
C++
// C++ implementation to print the pattern #include <bits/stdc++.h> using namespace std;
// Function to print required // number of stars void print1( int i)
{ for ( int j = 1; j <= i; j++)
cout << "*" ;
} void print( int n)
{ for ( int i = 1; i <= n; i++) {
// function calling to print
// the initial stars in each line
print1(i);
// printing underscore
cout << "_" ;
// function calling to print
// stars on right side of first half
print1(n - i + 1);
// printing underscore
cout << "_" ;
// function calling to print
// stars on left side of second half
print1(n - i + 1);
// printing underscore
cout << "_" ;
// function calling to print
// the ending stars
print1(i);
cout << endl;
}
} // Driver code int main()
{ // number of lines
int n = 5;
// function calling
print(n);
return 0;
} |
Java
// Java implementation to print the pattern import java.io.*;
class GFG
{ // Function to print required
// number of stars
static void print1( int i)
{
for ( int j = 1 ; j <= i; j++)
System.out.print( "*" );
}
static void print( int n)
{
for ( int i = 1 ; i <= n; i++) {
// function calling to print
// the initial stars in each line
print1(i);
// printing underscore
System.out.print( "_" );
// function calling to print
// stars on right side of first half
print1(n - i + 1 );
// printing underscore
System.out.print( "_" );
// function calling to print
// stars on left side of second half
print1(n - i + 1 );
// printing underscore
System.out.print( "_" );
// function calling to print
// the ending stars
print1(i);
System.out.println();
}
}
// Driver code
public static void main (String[] args)
{
// number of lines
int n = 5 ;
// function calling
print(n);
}
} // This code is contributed by vt_m. |
Python3
# Python3 implementation # to print the pattern # Function to print # required number of stars def print1(i):
for j in range ( 1 , i + 1 ):
print ( "*" , end = "")
def printPattern(n):
for i in range ( 1 , n + 1 ):
# function calling to
# print the initial
# stars in each line
print1(i)
# printing underscore
print ( "_" , end = "")
# function calling to
# print stars on right
# side of first half
print1(n - i + 1 )
# printing underscore
print ( "_" , end = "")
# function calling to
# print stars on left
# side of second half
print1(n - i + 1 )
# printing underscore
print ( "_" , end = "")
# function calling to print
# the ending stars
print1(i)
print ("")
# Driver code # number of lines n = 5
# function calling printPattern(n) # This code is contributed # by Smitha |
C#
// C# implementation to print the pattern using System;
class GFG {
// Function to print required
// number of stars
static void print1( int i)
{
for ( int j = 1; j <= i; j++)
Console.Write( "*" );
}
static void print( int n)
{
for ( int i = 1; i <= n; i++) {
// function calling to print
// the initial stars in each line
print1(i);
// printing underscore
Console.Write( "_" );
// function calling to print
// stars on right side of first half
print1(n - i + 1);
// printing underscore
Console.Write( "_" );
// function calling to print
// stars on left side of second half
print1(n - i + 1);
// printing underscore
Console.Write( "_" );
// function calling to print
// the ending stars
print1(i);
Console.WriteLine();
}
}
// Driver code
public static void Main ()
{
// number of lines
int n = 5;
// function calling
print(n);
}
} // This code is contributed by vt_m. |
PHP
<?php // PHP implementation for // expressionless face pattern function print1( $i )
{ for ( $j = 1; $j <= $i ; $j ++)
echo "*" ;
} function printp( $n )
{ for ( $i = 1; $i <= $n ; $i ++)
{
// function calling to print
// the initial stars in each line
print1( $i );
// printing underscore
echo "_" ;
// function calling to print
// stars on right side of first half
print1( $n - $i + 1);
// printing underscore
echo "_" ;
// function calling to print
// stars on left side of second half
print1( $n - $i + 1);
// printing underscore
echo "_" ;
// function calling to print
// the ending stars
print1( $i );
echo "\n" ;
}
} // Driver code $n = 5;
printp( $n );
// This code is contributed by Mithun Kumar ?> |
Javascript
<script> // javascript implementation to print the pattern // Function to print required // number of stars function print1(i)
{ for ( var j = 1; j <= i; j++)
document.write( "*" );
} function print(n)
{ for ( var i = 1; i <= n; i++) {
// function calling to print
// the initial stars in each line
print1(i);
// printing underscore
document.write( "_" );
// function calling to print
// stars on right side of first half
print1(n - i + 1);
// printing underscore
document.write( "_" );
// function calling to print
// stars on left side of second half
print1(n - i + 1);
// printing underscore
document.write( "_" );
// function calling to print
// the ending stars
print1(i);
document.write( '<br>' );
}
} // Driver code // number of lines var n = 5;
// function calling print(n); // This code is contributed by 29AjayKumar </script> |
Time complexity: O(n2), where n is the given integer.
Auxiliary space: O(1) using constant space for variables