Skip to main content

Display Hut Star Pattern

The "Display Hut Star Pattern" program is a C program that generates a hut-shaped pattern using stars (*). The program takes an input size and prints the corresponding hut pattern based on that size.

Problem Statement

The problem is to create a hut star pattern using asterisks. The pattern consists of a top shell, a bottom shell, and a base. The size of the pattern determines the number of rows and columns in the pattern.

For example, if the input size is 5, the pattern will be:

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

The hut pattern has a top shell that forms an inverted pyramid of stars, a bottom shell that forms a pyramid of stars, and a base that consists of three rows of stars with empty spaces in the middle.

Algorithm and Pseudocode

1. Start the main function.

2. Define the function space to print spaces. It takes an input size and prints spaces equal to the given size.

3. Define the function print_star to print stars. It takes an input size and prints stars equal to the given size.

4. Define the function hut_star_pattern to generate the hut pattern. It takes an input size and follows the following steps:

  • a. Print the size of the pattern.
  • b. Use a loop to print the top shell of the hut pattern. The loop iterates from 0 to size-1.
  • c. Inside the loop, call the space function to print the required number of spaces.
  • d. Call the print_star function to print the required number of stars.
  • e. Print a newline character to move to the next line.
  • f. Use a loop to print the bottom shell of the hut pattern. The loop iterates 3 times.
  • g. Inside the loop, use another loop to print each line of the bottom shell.
  • h. Check the conditions to print stars or spaces based on the position.
  • i. Print a newline character to move to the next line.

5. In the main function, call the hut_star_pattern function with different test cases.

6. End the main function.

Code Solution

Here given code implementation process.

//C Program 
//Display Hut Star pattern
#include <stdio.h>
#include <stdlib.h>

//Include Space of given size
void space(int size) {

  int counter = 0;

  for (counter = 0; counter < size; counter++) {

    //Add space
    printf(" ");
  }
}
//Include star of given size
void print_star(int size) {

  int counter = 0;

  for (counter = 0; counter < size; counter++) {

    //Add space
    printf("*");
  }
}

//Display the hut star pattern of given size
void hut_star_pattern(int size) {

  
  printf("\n Size : %d \n\n", size);
  int i = 0;
  int j = 0; 
 
  //Display top shell of result
  for (i = 0; i < size; i++) {
    space(size-i-1);
    print_star((i+i)+1);
    printf("\n");
  }
  //print the bottom shell of result
  for (int k = 0; k < 3; ++k)
  {
    for (j = 0; j < size+size-1; j++) {

      if(j<=2 || j>=(size+size-1)-3)
      {
        printf("*");
      }
      else
      {
        printf(" ");
      }
    }
    printf("\n");
  }
}


int main() {
  //Test Cases
  hut_star_pattern(5);
  hut_star_pattern(4);
  hut_star_pattern(7);
  return 0;
}

Output

 Size : 5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size : 4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size : 7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
/*
  C++ Program
  Display hut star pattern
*/
#include<iostream>

using namespace std;
class MyPattern {
	public:

    //Include Space of given size
    void space(int size) {
      int counter = 0;
      for (counter = 0; counter < size; counter++) {
        //Add space

        cout << " ";
      }
    }
	//Include star of given size
	void print_star(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			//Add space

			cout << "*";
		}
	}
	//Display the hut star pattern of given size
	void hut_star_pattern(int size) {
		cout << "\n Size : " << size << " \n\n";
		int i = 0;
		int j = 0;
		//Display top shell of result

		for (i = 0; i < size; i++) {
			this->space(size - i - 1);
			this->print_star((i + i) + 1);
			cout << "\n";
		}
		//print the bottom shell of result

		for (int k = 0; k < 3; ++k) {
			for (j = 0; j < size + size - 1; j++) {
				if (j <= 2 ||
					j >= (size + size - 1) - 3) {
					cout << "*";
				} else {
					cout << " ";
				}
			}
			cout << "\n";
		}
	}
};
int main() {
	MyPattern obj =  MyPattern();
	//Test Cases
	obj.hut_star_pattern(5);
	obj.hut_star_pattern(4);
	obj.hut_star_pattern(7);
	return 0;
}

Output

 Size : 5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size : 4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size : 7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
/*
  Java Program
  Display hut star pattern
*/

public class MyPattern {

  //Include Space of given size
  public void space(int size) {

    int counter = 0;

    for (counter = 0; counter < size; counter++) {

      //Add space
      System.out.print(" ");
    }
  }
  //Include star of given size
  public void print_star(int size) {

    int counter = 0;

    for (counter = 0; counter < size; counter++) {

      //Add space
      System.out.print("*");
    }
  }

  //Display the hut star pattern of given size
  public void hut_star_pattern(int size) {

    
    System.out.print("\n Size : "+size+" \n\n");
    int i = 0;
    int j = 0; 
   
    //Display top shell of result
    for (i = 0; i < size; i++) {
      space(size-i-1);
      print_star((i+i)+1);
      System.out.print("\n");
    }
    //print the bottom shell of result
    for (int k = 0; k < 3; ++k)
    {
      for (j = 0; j < size+size-1; j++) {

        if(j<=2 || j>=(size+size-1)-3)
        {
          System.out.print("*");
        }
        else
        {
          System.out.print(" ");
        }
      }
      System.out.print("\n");
    }
  }
  public static void main(String[] args) {

    MyPattern obj = new MyPattern();
    //Test Cases
    obj.hut_star_pattern(5);
    obj.hut_star_pattern(4);
    obj.hut_star_pattern(7);
  }
}

Output

 Size : 5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size : 4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size : 7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
/*
  C# Program
  Display hut star pattern
*/
using System;

public class MyPattern {
	//Include Space of given size
	public void space(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			Console.Write(" ");
		}
	}
	//Include star of given size
	public void print_star(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			Console.Write("*");
		}
	}
	//Display the hut star pattern of given size
	public void hut_star_pattern(int size) {
		Console.Write("\n Size : " + size + " \n\n");
		int i = 0;
		int j = 0;
		//Display top shell of result

		for (i = 0; i < size; i++) {
			space(size - i - 1);
			print_star((i + i) + 1);
			Console.Write("\n");
		}
		//print the bottom shell of result

		for (int k = 0; k < 3; ++k) {
			for (j = 0; j < size + size - 1; j++) {
				if (j <= 2 ||
					j >= (size + size - 1) - 3) {
					Console.Write("*");
				} else {
					Console.Write(" ");
				}
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args) {
		MyPattern obj = new MyPattern();
		obj.hut_star_pattern(5);
		obj.hut_star_pattern(4);
		obj.hut_star_pattern(7);
	}
}

Output

 Size : 5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size : 4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size : 7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
<?php
/*
  Php Program
  Display hut star pattern
*/
class MyPattern {
	//Include Space of given size

	public 	function space($size) {
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++) {
			//Add space

			echo(" ");
		}
	}
	//Include star of given size

	public 	function print_star($size) {
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++) {
			//Add space

			echo("*");
		}
	}
	//Display the hut star pattern of given size

	public 	function hut_star_pattern($size) {
		echo("\n Size : ". $size ." \n\n");
		$i = 0;
		$j = 0;
		//Display top shell of result

		for ($i = 0; $i < $size; $i++) {
			$this->space($size - $i - 1);
			$this->print_star(($i + $i) + 1);
			echo("\n");
		}
		//print the bottom shell of result

		for ($k = 0; $k < 3; ++$k) {
			for ($j = 0; $j < $size + $size - 1; $j++) {
				if ($j <= 2 ||
					$j >= ($size + $size - 1) - 3) {
					echo("*");
				} else {
					echo(" ");
				}
			}
			echo("\n");
		}
	}
}

function main() {
	$obj = new MyPattern();
	//Test Cases

	$obj->hut_star_pattern(5);
	$obj->hut_star_pattern(4);
	$obj->hut_star_pattern(7);

}
main();

Output

 Size : 5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size : 4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size : 7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
/*
  Node Js Program
  Display hut star pattern
*/
class MyPattern {
	//Include Space of given size
	space(size) {
		var counter = 0;
		for (counter = 0; counter < size; counter++) {
			//Add space

			process.stdout.write(" ");
		}
	}

	//Include star of given size
	print_star(size) {
		var counter = 0;
		for (counter = 0; counter < size; counter++) {
			//Add space

			process.stdout.write("*");
		}
	}

	//Display the hut star pattern of given size
	hut_star_pattern(size) {
		process.stdout.write("\n Size : " + size + " \n\n");
		var i = 0;
		var j = 0;
		//Display top shell of result

		for (i = 0; i < size; i++) {
			this.space(size - i - 1);
			this.print_star((i + i) + 1);
			process.stdout.write("\n");
		}

		//print the bottom shell of result

		for (var k = 0; k < 3; ++k) {
			for (j = 0; j < size + size - 1; j++) {
				if (j <= 2 ||
					j >= (size + size - 1) - 3) {
					process.stdout.write("*");
				} else {
					process.stdout.write(" ");
				}
			}

			process.stdout.write("\n");
		}
	}
}

function main(args) {
	var obj = new MyPattern();
	//Test Cases
	obj.hut_star_pattern(5);
	obj.hut_star_pattern(4);
	obj.hut_star_pattern(7);
}

main();

Output

 Size : 5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size : 4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size : 7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
# Python 3 Program
# Display hut star pattern

class MyPattern :
	# Include Space of given size
	def space(self, size) :
		counter = 0
		while (counter < size) :
			print(" ", end = "")
			counter += 1
		
	
	# Include star of given size
	def print_star(self, size) :
		counter = 0
		while (counter < size) :
			print("*", end = "")
			counter += 1
		
	
	# Display the hut star pattern of given size
	def hut_star_pattern(self, size) :
		print("\n Size : ", size ," \n\n", end = "")
		i = 0
		j = 0
		# Display top shell of result
		while (i < size) :
			self.space(size - i - 1)
			self.print_star((i + i) + 1)
			print("\n", end = "")
			i += 1
		
		# print the bottom shell of result
		k = 0
		while (k < 3) :
			j = 0
			while (j < size + size - 1) :
				if (j <= 2 or j >= (size + size - 1) - 3) :
					print("*", end = "")
				else :
					print(" ", end = "")
				
				j += 1
			
			print("\n", end = "")
			k += 1
		
	

def main() :
	obj = MyPattern()
	obj.hut_star_pattern(5)
	obj.hut_star_pattern(4)
	obj.hut_star_pattern(7)


if __name__ == "__main__":
	main()

Output

 Size :  5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size :  4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size :  7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
#   Ruby Program
#   Display hut star pattern

class MyPattern 
	 # Include Space of given size
	def space(size) 
		counter = 0
		while (counter < size) 
			print(" ")
			counter += 1
		end
	end
	 # Include star of given size
	def print_star(size) 
		counter = 0
		while (counter < size) 
			print("*")
			counter += 1
		end
	end
	 # Display the hut star pattern of given size
	def hut_star_pattern(size) 
		print("\n Size  :", size ," \n\n")
		i = 0
		j = 0
		 # Display top shell of result
		while (i < size) 
			self.space(size - i - 1)
			self.print_star((i + i) + 1)
			print("\n")
			i += 1
		end
		 # print the bottom shell of result
		k = 0
		while (k < 3) 
			j = 0
			while (j < size + size - 1) 
				if (j <= 2 ||
					j >= (size + size - 1) - 3) 
					print("*")
				else 
					print(" ")
				end
				j += 1
			end
			print("\n")
			k += 1
		end
	end
end
def main() 
	obj = MyPattern.new()
	obj.hut_star_pattern(5)
	obj.hut_star_pattern(4)
	obj.hut_star_pattern(7)
end
main()

Output

 Size  :5 

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size  :4 

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size  :7 

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
/*
  Scala Program
  Display hut star pattern
*/
class MyPattern {
	//Include Space of given size
	def space(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size) {
			print(" ");
			counter += 1;
		}
	}
	//Include star of given size
	def print_star(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size) {
			print("*");
			counter += 1;
		}
	}
	//Display the hut star pattern of given size
	def hut_star_pattern(size: Int): Unit = {
		print("\n Size : " + size + " \n\n");
		var i: Int = 0;
		var j: Int = 0;

		//Display top shell of result
		while (i < size) {
			this.space(size - i - 1);
			this.print_star((i + i) + 1);
			print("\n");
			i += 1;
		}
		//print the bottom shell of result
		var k: Int = 0;
		while (k < 3) {
			j = 0;
			while (j < size + size - 1) {
				if (j <= 2 ||
					j >= (size + size - 1) - 3) {
					print("*");
				} else {
					print(" ");
				}
				j += 1;
			}
			print("\n");
			k += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyPattern = new MyPattern();
		obj.hut_star_pattern(5);
		obj.hut_star_pattern(4);
		obj.hut_star_pattern(7);
	}
}

Output

 Size : 5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size : 4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size : 7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***
/*
  Swift Program
  Display hut star pattern
*/
class MyPattern {
	//Include Space of given size
	func space(_ size: Int) {
		var counter = 0;
		while (counter < size) {
			print(" ", terminator: "");
			counter += 1;
		}
	}
	//Include star of given size
	func print_star(_ size: Int) {
		var counter = 0;
		while (counter < size) {
			print("*", terminator: "");
			counter += 1;
		}
	}
	//Display the hut star pattern of given size
	func hut_star_pattern(_ size: Int) {
		print("\n Size : ", size ," \n\n", terminator: "");
		var i = 0;
		var j = 0;
		//Display top shell of result
		while (i < size) {
			self.space(size - i - 1);
			self.print_star((i + i) + 1);
			print("\n", terminator: "");
			i += 1;
		}
		//print the bottom shell of result
		var k = 0;
		while (k < 3) {
			j = 0;
			while (j < size + size - 1) {
				if (j <= 2 ||
					j >= (size + size - 1) - 3) {
					print("*", terminator: "");
				} else {
					print(" ", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			k += 1;
		}
	}
}
func main() {
	let obj = MyPattern();
	obj.hut_star_pattern(5);
	obj.hut_star_pattern(4);
	obj.hut_star_pattern(7);
}
main();

Output

 Size :  5

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

 Size :  4

   *
  ***
 *****
*******
*** ***
*** ***
*** ***

 Size :  7

      *
     ***
    *****
   *******
  *********
 ***********
*************
***       ***
***       ***
***       ***

Time Complexity

The time complexity of the program is O(n^2), where n is the input size. The program uses nested loops to print the pattern, with the outer loop running size times and the inner loop running (size+size-1) times. Therefore, the time complexity is quadratic.

Output Explanation

The program generates the hut star pattern for different input sizes. The output is displayed with the corresponding size mentioned at the top.

For example, when the size is 5, the pattern is:

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

Here, the top shell has a base of 5 stars, and each subsequent row has two additional stars. The bottom shell has three rows with a base of 7 stars. The base has three rows with three stars and three spaces in between.

Similarly, the program generates patterns for input sizes 4 and 7, and displays them along with the respective sizes.

The generated patterns follow the algorithm described earlier and create the hut-shaped star pattern with the given sizes.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment