Program to print Hut Star pattern
Given a number N such that N >= 5. The task is to print a Hut Star Pattern with (N + 3) rows as shown in the below examples.
Examples:
Input: N = 5
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
Input: N = 7
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
Approach: The upper N rows of the pattern will be a triangle and the last 3 rows will contain two space-separated rectangles as shown in the above examples.
- Firstly print the upper triangle with N rows.
- Then print the 2 lower rectangles of the Hut in 3 rows.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void printHutStar( int n)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++)
cout << " " ;
for (j = 0; j < (2 * i + 1); j++)
cout << "*" ;
cout << endl ;
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
cout << "*" ;
for (j = 0; j < (2 * n - 7); j++)
cout << " " ;
for (j = 0; j < 3; j++)
cout << "*" ;
cout << endl ;
}
}
int main()
{
int n = 7;
printHutStar(n);
return 0;
}
|
Java
public class GFG {
static void printHutStar( int n)
{
int i, j;
for (i = 0 ; i < n; i++) {
for (j = i + 1 ; j < n; j++)
System.out.print( " " );
for (j = 0 ; j < ( 2 * i + 1 ); j++)
System.out.print( "*" );
System.out.println();
}
for (i = 0 ; i < 3 ; i++) {
for (j = 0 ; j < 3 ; j++)
System.out.print( "*" );
for (j = 0 ; j < ( 2 * n - 7 ); j++)
System.out.print( " " );
for (j = 0 ; j < 3 ; j++)
System.out.print( "*" );
System.out.println();
}
}
public static void main(String args[])
{
int n = 7 ;
printHutStar(n);
}
}
|
Python3
def printHutStar(n):
for i in range (n):
for j in range (i + 1 , n):
print ( ' ' , end = '')
for j in range ( 0 , 2 * i + 1 ):
print ( '*' , end = '')
print ()
for i in range ( 3 ):
for j in range ( 3 ):
print ( '*' , end = '')
for j in range ( 2 * n - 7 ):
print ( ' ' , end = '')
for j in range ( 3 ):
print ( '*' , end = '')
print ()
n = 7
printHutStar(n)
|
C#
using System;
class GFG {
static void printHutStar( int n)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++)
Console.Write( " " );
for (j = 0; j < (2 * i + 1); j++)
Console.Write( "*" );
Console.Write( "\n" );
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
Console.Write( "*" );
for (j = 0; j < (2 * n - 7); j++)
Console.Write( " " );
for (j = 0; j < 3; j++)
Console.Write( "*" );
Console.Write( "\n" );
}
}
public static void Main()
{
int n = 7;
printHutStar(n);
}
}
|
PHP
<?php
function printHutStar( $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i + 1; $j < $n ; $j ++)
echo " " ;
for ( $j = 0; $j < (2 * $i + 1); $j ++)
echo "*" ;
echo "\n" ;
}
for ( $i = 0; $i < 3; $i ++)
{
for ( $j = 0; $j < 3; $j ++)
echo "*" ;
for ( $j = 0; $j < (2 * $n - 7); $j ++)
echo " " ;
for ( $j = 0; $j < 3; $j ++)
echo "*" ;
echo "\n" ;
}
}
$n = 7;
printHutStar( $n );
|
Javascript
<script>
function printHutStar(n)
{
var i, j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++)
document.write( " " );
for (j = 0; j < 2 * i + 1; j++)
document.write( "*" );
document.write( "<br>" );
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
document.write( "*" );
for (j = 0; j < 2 * n - 7; j++)
document.write( " " );
for (j = 0; j < 3; j++)
document.write( "*" );
document.write( "<br>" );
}
}
var n = 7;
printHutStar(n);
</script>
|
Output:
*
***
*****
*******
*********
***********
*************
*** ***
*** ***
*** ***
Time Complexity: O(n*n*n)
Auxiliary Space: O(1)
Another method: (Reduce time complexity)
C++
#include<iostream>
using namespace std;
int main()
{
int n=7,i,j;
for (i=0,j=0;i<n;j++)
{
if (j==n+i)
{
j=-1;
++i;
cout<< "\n" ;
}
else if ((i+j)>=n-1)
{
cout<< "*" ;
}
else
cout<< " " ;
}
for ( int k=0;k<3;k++)
{
for ( int l=0;l<n-1+i;l++)
{
if (l<=2 || (l<=n-1+i && l>=n-4+i))
cout<< "*" ;
else
cout<< " " ;
}
cout<<endl;
}
}
|
Java
class GFG
{
public static void main(String args[])
{
int n = 7 , i, j;
for (i = 0 , j = 0 ; i < n; j++)
{
if (j == n + i)
{
j = - 1 ;
++i;
System.out.print( "\n" );
}
else if ((i + j) >= n - 1 )
{
System.out.print( "*" );
}
else
{
System.out.print( " " );
}
}
for ( int k = 0 ; k < 3 ; k++)
{
for ( int l = 0 ; l < n - 1 + i; l++)
{
if (l <= 2 || (l <= n - 1 + i &&
l >= n - 4 + i))
{
System.out.print( "*" );
}
else
{
System.out.print( " " );
}
}
System.out.print( "\n" );
}
}
}
|
Python3
import math as mt
if __name__ = = '__main__' :
n = 7
i, j = 0 , 0
while (i < n):
if (j = = n + i):
j = - 1
i + = 1
print ()
elif ((i + j) > = n - 1 ):
print ( "*" , end = "")
else :
print (end = " " )
j + = 1
for k in range ( 3 ):
for l in range (n - 1 + i):
if (l < = 2 or (l < = n - 1 + i and
l > = n - 4 + i)):
print ( "*" , end = "")
else :
print (end = " " )
print ()
|
C#
using System;
class GFG
{
public static void Main()
{
int n = 7, i, j;
for (i = 0, j = 0; i < n; j++)
{
if (j == n + i)
{
j = -1;
++i;
Console.Write( "\n" );
}
else if ((i + j) >= n - 1)
{
Console.Write( "*" );
}
else
{
Console.Write( " " );
}
}
for ( int k = 0; k < 3; k++)
{
for ( int l = 0; l < n - 1 + i; l++)
{
if (l <= 2 || (l <= n - 1 + i &&
l >= n - 4 + i))
{
Console.Write( "*" );
}
else
{
Console.Write( " " );
}
}
Console.Write( "\n" );
}
}
}
|
PHP
<?php
$n = 7;
for ( $i = 0, $j = 0; $i < $n ; $j ++)
{
if ( $j == $n + $i )
{
$j = -1;
++ $i ;
echo ( "\n" );
}
else if (( $i + $j ) >= $n - 1)
{
echo ( "*" );
}
else
{
echo ( " " );
}
}
for ( $k = 0; $k < 3; $k ++)
{
for ( $l = 0; $l < $n - 1 + $i ; $l ++)
{
if ( $l <= 2 || ( $l <= $n - 1 + $i &&
$l >= $n - 4 + $i ))
{
echo ( "*" );
}
else
{
echo ( " " );
}
}
echo ( "\n" );
}
?>
|
Javascript
<script>
var n = 7, i, j;
for (i = 0, j = 0; i < n; j++)
{
if (j == n + i)
{
j = -1;
++i;
document.write( "<br/>" );
}
else if ((i + j) >= n - 1)
{
document.write( "*" );
}
else
{
document.write( " " );
}
}
for (k = 0; k < 3; k++)
{
for (l = 0; l < n - 1 + i; l++)
{
if (l <= 2 || (l <= n - 1 + i &&
l >= n - 4 + i))
{
document.write( "*" );
}
else
{
document.write( " " );
}
}
document.write( "<br/>" );
}
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Last Updated :
08 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...