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++
| // C++ program to print// the Hut Star Pattern#include <iostream>usingnamespacestd;// Function to print the Hut-Star PatternvoidprintHutStar(intn){    inti, j;    // Printing the upper triangle    for(i = 0; i < n; i++) {        // Left space triangle        for(j = i + 1; j < n; j++)            cout << " ";        // Center Star triangle        for(j = 0; j < (2 * i + 1); j++)            cout << "*";        cout << endl ;    }    // Printing Lower rectangles    for(i = 0; i < 3; i++) {        // Left rectangle        for(j = 0; j < 3; j++)            cout << "*";        // Center Space rectangle        for(j = 0; j < (2 * n - 7); j++)            cout << " ";        // Right rectangle        for(j = 0; j < 3; j++)            cout << "*";        cout << endl ;    }}intmain(){intn = 7;    // function calling    printHutStar(n);return0;// This code is contributed// by ANKITRAI1} | 
Java
| // Java program to print// the Hut Star Pattern  publicclassGFG {      // Function to print the Hut-Star Pattern    staticvoidprintHutStar(intn)    {        inti, j;          // Printing the upper triangle        for(i = 0; i < n; i++) {              // Left space triangle            for(j = i + 1; j < n; j++)                System.out.print(" ");              // Center Star triangle            for(j = 0; j < (2* i + 1); j++)                System.out.print("*");              System.out.println();        }          // Printing Lower rectangles        for(i = 0; i < 3; i++) {              // Left rectangle            for(j = 0; j < 3; j++)                System.out.print("*");              // Center Space rectangle            for(j = 0; j < (2* n - 7); j++)                System.out.print(" ");              // Right rectangle            for(j = 0; j < 3; j++)                System.out.print("*");              System.out.println();        }    }      // Driver Code    publicstaticvoidmain(String args[])    {        intn = 7;          printHutStar(n);    }} | 
Python3
| # Python program to print# the Hut Star Pattern# Function to print the Hut-Star PatterndefprintHutStar(n):    # Printing the upper triangle    fori inrange(n):        # Left space triangle        forj inrange(i +1, n):            print(' ', end ='')        # Center Star triangle        forj inrange(0, 2*i +1):            print('*', end ='')        print()    # Printing the lower rectangles    fori inrange(3):        # Left rectangle        forj inrange(3):            print('*', end ='')         # Center Space rectangle        forj inrange(2*n -7):            print(' ', end ='')        # Right rectangle        forj inrange(3):            print('*', end ='')        print()# Driver Coden =7# function callingprintHutStar(n)# This code is contributed# by SamyuktaSHegde | 
C#
| // C# program to print// the Hut Star PatternusingSystem;classGFG {    // Function to print the Hut-Star Pattern    staticvoidprintHutStar(intn)    {        inti, j;        // Printing the upper triangle        for(i = 0; i < n; i++) {            // Left space triangle            for(j = i + 1; j < n; j++)                Console.Write(" ");            // Center Star triangle            for(j = 0; j < (2 * i + 1); j++)                Console.Write("*");            Console.Write("\n");        }        // Printing Lower rectangles        for(i = 0; i < 3; i++) {            // Left rectangle            for(j = 0; j < 3; j++)                Console.Write("*");            // Center Space rectangle            for(j = 0; j < (2 * n - 7); j++)                Console.Write(" ");            // Right rectangle            for(j = 0; j < 3; j++)                Console.Write("*");            Console.Write("\n");        }    }    // Driver Code    publicstaticvoidMain()    {        intn = 7;        printHutStar(n);    }}// This code is contributed by ChitraNayal | 
PHP
| <?php// PHP program to print// the Hut Star Pattern// Function to print the Hut-Star PatternfunctionprintHutStar($n){    // Printing the upper triangle    for($i= 0; $i< $n; $i++)    {        // Left space triangle        for($j= $i+ 1; $j< $n; $j++)            echo" ";        // Center Star triangle        for($j= 0; $j< (2 * $i+ 1); $j++)            echo"*";        echo"\n";    }    // Printing Lower rectangles    for($i= 0; $i< 3; $i++)    {        // Left rectangle        for($j= 0; $j< 3; $j++)            echo"*";        // Center Space rectangle        for($j= 0; $j< (2 * $n- 7); $j++)            echo" ";        // Right rectangle        for($j= 0; $j< 3; $j++)            echo"*";        echo"\n";    }}// Driver Code$n= 7;// function callingprintHutStar($n);// This code is contributed// by Akanksha Rai | 
Javascript
| <script>      // JavaScript program to print      // the Hut Star Pattern      // Function to print the Hut-Star Pattern      functionprintHutStar(n)      {        vari, j;        // Printing the upper triangle        for(i = 0; i < n; i++) {          // Left space triangle          for(j = i + 1; j < n; j++)          document.write("  ");          // Center Star triangle          for(j = 0; j < 2 * i + 1; j++)          document.write("*");          document.write("<br>");        }        // Printing Lower rectangles        for(i = 0; i < 3; i++) {          // Left rectangle          for(j = 0; j < 3; j++)          document.write("*");          // Center Space rectangle          for(j = 0; j < 2 * n - 7; j++)          document.write("  ");          // Right rectangle          for(j = 0; j < 3; j++)          document.write("*");          document.write("<br>");        }      }      varn = 7;            // function calling      printHutStar(n);          </script> | 
      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
Time Complexity: O(n*n*n)
Auxiliary Space: O(1)
Another method: (Reduce time complexity)
C++
| // C++ program to print// the Hut Star Pattern#include<iostream>usingnamespacestd;intmain(){    intn=7,i,j;        //LOOP FOR HUT PYRAMID    for(i=0,j=0;i<n;j++)            {        if(j==n+i)        {            j=-1;            ++i;            cout<<"\n";        }                // print Center Star triangle        elseif((i+j)>=n-1)        {            cout<<"*";        }        else            cout<<" ";    }        //LOOPS FOR LOWER WALLS    for(intk=0;k<3;k++)            {        // Left and right rectangle        for(intl=0;l<n-1+i;l++)        {            if(l<=2 || (l<=n-1+i && l>=n-4+i))                cout<<"*";            else                cout<<" ";        }        cout<<endl;    }}//This code is contributed by KUMARAN | 
Java
| // Java program to print the Hut Star PatternclassGFG{publicstaticvoidmain(String args[]){    intn = 7, i, j;    // LOOP FOR HUT PYRAMID    for(i = 0, j = 0; i < n; j++)    {        if(j == n + i)        {            j = -1;            ++i;            System.out.print("\n");        } // print Center Star triangle        elseif((i + j) >= n - 1)        {            System.out.print("*");        }        else        {            System.out.print(" ");        }    }    // LOOPS FOR LOWER WALLS    for(intk = 0; k < 3; k++)    {        // Left and right rectangle        for(intl = 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");    }}}// This code is contributed// by PrinciRaj1992 | 
Python3
| # Python3 program to print the# Hut Star Patternimportmath as mtif__name__ =='__main__':    n =7    i, j =0, 0        # LOOP FOR HUT PYRAMID    while(i < n):            if(j ==n +i):            j =-1            i +=1            print()                    elif((i +j) >=n -1):            print("*", end ="")        else:            print(end =" ")        j +=1        # LOOPS FOR LOWER WALLS    fork inrange(3):                   # Left and right rectangle        forl inrange(n -1+i):            if(l <=2or(l <=n -1+i and                          l >=n -4+i)):                print("*", end ="")            else:                print(end =" ")                print()    # This code is contributed by# Mohit kumar 29 | 
C#
| // C# program to print the Hut Star PatternusingSystem;classGFG{publicstaticvoidMain(){    intn = 7, i, j;    // LOOP FOR HUT PYRAMID    for(i = 0, j = 0; i < n; j++)    {        if(j == n + i)        {            j = -1;            ++i;            Console.Write("\n");        } // print Center Star triangle        elseif((i + j) >= n - 1)        {            Console.Write("*");        }        else        {            Console.Write(" ");        }    }    // LOOPS FOR LOWER WALLS    for(intk = 0; k < 3; k++)    {        // Left and right rectangle        for(intl = 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");    }}}// This code is contributed// by Akanksha Rai | 
PHP
| <?php// PHP program to print the// Hut Star Pattern// Driver Code$n= 7;// LOOP FOR HUT PYRAMIDfor($i= 0, $j= 0; $i< $n; $j++){    if($j== $n+ $i)    {        $j= -1;        ++$i;        echo("\n");    } // print Center Star triangle    elseif(($i+ $j) >= $n- 1)    {        echo("*");    }    else    {        echo(" ");    }}// LOOPS FOR LOWER WALLSfor($k= 0; $k< 3; $k++){    // Left and right rectangle    for($l= 0; $l< $n- 1 + $i; $l++)    {        if($l<= 2 || ($l<= $n- 1 + $i&&                        $l>= $n- 4 + $i))        {            echo("*");        }        else        {            echo(" ");        }    }    echo("\n");}// This code is contributed// by Mukul singh?> | 
Javascript
| <script>// Javascript program to print the Hut Star Pattern    varn = 7, i, j;// LOOP FOR HUT PYRAMIDfor(i = 0, j = 0; i < n; j++){    if(j == n + i)    {        j = -1;        ++i;        document.write("<br/>");    }        // Print Center Star triangle    elseif((i + j) >= n - 1)    {        document.write("*");    }    else    {        document.write("  ");    }}// LOOPS FOR LOWER WALLSfor(k = 0; k < 3; k++){        // Left and right rectangle    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/>");}// This code is contributed by umadevi9616</script> | 
Time Complexity: O(n2)
Auxiliary Space: O(1)


 
                                    







