Print Hut Star pattern
In this article, we will discuss how to print a hut star pattern using C programming language. We will provide an explanation of the problem statement, present a suitable example, discuss the algorithm and pseudocode, and analyze the resultant output with the time complexity of the code.
Problem Statement
The problem is to print a hut star pattern using asterisks (*) and spaces. The pattern consists of layers representing the roof and walls of a hut.
Example
Let's consider an example with a size of 5:
SIZE : 5 * * * * * * * * * * * * * * * * * * * * * * *
In the above example, the pattern is printed with a size of 5. The top layer represents the roof of the hut, and the bottom layers represent the walls.
Algorithm and Pseudocode
Here is the algorithm to print the hut star pattern:
1. Define the functions: - space(size) to print spaces. - print_symbol(size) to print asterisks. - print_hut(size) to print the entire hut pattern. 2. Check if the given size is less than or equal to 2. If so, return. 3. Print the top layer of the hut: - Iterate from i = 0 to size-1. - Print spaces (size - i + 1). - Print asterisks (i + 1) followed by a space. - Print a new line. 4. Calculate the base size for the bottom layers: - Set base = size / 4 + 1. - If base is less than or equal to 1, set base = 2. 5. Print the bottom layers of the hut: - Iterate from i = 0 to size/2 - 1. - Print 2 spaces. - Print asterisks (base) representing the walls. - Print spaces ((size) * 2 - (base * 2) * 2) to create the gap between walls. - Print another set of asterisks (base) representing the walls. - Print a new line.
Code Solution
Here given code implementation process.
//C Program
//Print Hut Star pattern
#include <stdio.h>
/*Include Space of given size*/
void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
printf(" ");
}
}
void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
printf("* ");
}
}
void print_hut(int size)
{
if (size <= 2)
{
return;
}
printf("\nSIZE : %d\n\n", size);
int i = 0;
int base = size / 4 + 1;
//Print the top layer
for (i = 0; i < size; ++i)
{
space(size - i + 1);
print_symbol(i + 1);
printf("\n");
}
if (base <= 1)
{
base = 2;
}
//Print bottom layers
for (i = 0; i < size / 2; ++i)
{
space(2);
print_symbol(base);
space((size) * 2 - (base * 2) * 2);
print_symbol(base);
printf("\n");
}
}
int main()
{
//Simple test
print_hut(5);
print_hut(8);
print_hut(9);
print_hut(13);
return 0;
}
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
/*
Java Program
Print Hut Star pattern
*/
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
System.out.print(" ");
}
}
public void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
System.out.print("* ");
}
}
public void print_hut(int size)
{
if (size <= 2)
{
return;
}
System.out.print("\nSIZE : " + size + "\n\n");
int i = 0;
int base_support = size / 4 + 1;
//Print the top layer
for (i = 0; i < size; ++i)
{
space(size - i + 1);
print_symbol(i + 1);
System.out.print("\n");
}
if (base_support <= 1)
{
base_support = 2;
}
//Print bottom layers
for (i = 0; i < size / 2; ++i)
{
space(2);
print_symbol(base_support);
space((size) * 2 - (base_support * 2) * 2);
print_symbol(base_support);
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_hut(5);
obj.print_hut(8);
obj.print_hut(9);
obj.print_hut(13);
}
}
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
/*
C++ Program
Print Hut Star pattern
*/
#include<iostream>
using namespace std;
class MyPattern
{
public: void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << " ";
}
}
void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << "* ";
}
}
void print_hut(int size)
{
if (size <= 2)
{
return;
}
cout << "\nSIZE : " << size << "\n\n";
int i = 0;
int base_support = size / 4 + 1;
//Print the top layer
for (i = 0; i < size; ++i)
{
this->space(size - i + 1);
this->print_symbol(i + 1);
cout << "\n";
}
if (base_support <= 1)
{
base_support = 2;
}
//Print bottom layers
for (i = 0; i < size / 2; ++i)
{
this->space(2);
this->print_symbol(base_support);
this->space((size) * 2 - (base_support * 2) * 2);
this->print_symbol(base_support);
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.print_hut(5);
obj.print_hut(8);
obj.print_hut(9);
obj.print_hut(13);
return 0;
}
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
/*
C# Program
Print Hut Star pattern
*/
using System;
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
Console.Write(" ");
}
}
public void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
Console.Write("* ");
}
}
public void print_hut(int size)
{
if (size <= 2)
{
return;
}
Console.Write("\nSIZE : " + size + "\n\n");
int i = 0;
int base_support = size / 4 + 1;
//Print the top layer
for (i = 0; i < size; i++)
{
space(size - i + 1);
print_symbol(i + 1);
Console.Write("\n");
}
if (base_support <= 1)
{
base_support = 2;
}
//Print bottom layers
for (i = 0; i < size / 2; i++)
{
space(2);
print_symbol(base_support);
space((size) * 2 - (base_support * 2) * 2);
print_symbol(base_support);
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_hut(5);
obj.print_hut(8);
obj.print_hut(9);
obj.print_hut(13);
}
}
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
<?php
/*
Php Program
Print Hut Star pattern
*/
class MyPattern
{
public function space($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
//Add space
echo(" ");
}
}
public function print_symbol($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
echo("* ");
}
}
public function print_hut($size)
{
if ($size <= 2)
{
return;
}
echo("\nSIZE : ". $size ."\n\n");
$i = 0;
$base_support = intval($size / 4) + 1;
//Print the top layer
for ($i = 0; $i < $size; ++$i)
{
$this->space($size - $i + 1);
$this->print_symbol($i + 1);
echo("\n");
}
if ($base_support <= 1)
{
$base_support = 2;
}
//Print bottom layers
for ($i = 0; $i < intval($size / 2); ++$i)
{
$this->space(2);
$this->print_symbol($base_support);
$this->space(($size) * 2 - ($base_support * 2) * 2);
$this->print_symbol($base_support);
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->print_hut(5);
$obj->print_hut(8);
$obj->print_hut(9);
$obj->print_hut(13);
}
main();
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
/*
Node Js Program
Print Hut Star pattern
*/
class MyPattern
{
space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
process.stdout.write(" ");
}
}
print_symbol(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
process.stdout.write("* ");
}
}
print_hut(size)
{
if (size <= 2)
{
return;
}
process.stdout.write("\nSIZE : " + size + "\n\n");
var i = 0;
var base_support = parseInt(size / 4) + 1;
//Print the top layer
for (i = 0; i < size; ++i)
{
this.space(size - i + 1);
this.print_symbol(i + 1);
process.stdout.write("\n");
}
if (base_support <= 1)
{
base_support = 2;
}
//Print bottom layers
for (i = 0; i < parseInt(size / 2); ++i)
{
this.space(2);
this.print_symbol(base_support);
this.space((size) * 2 - (base_support * 2) * 2);
this.print_symbol(base_support);
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Simple test
obj.print_hut(5);
obj.print_hut(8);
obj.print_hut(9);
obj.print_hut(13);
}
main();
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
# Python 3 Program
# Print Hut Star pattern
class MyPattern :
def space(self, size) :
counter = 0
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
def print_symbol(self, size) :
counter = 0
counter = 0
while (counter < size) :
print("* ", end = "")
counter += 1
def print_hut(self, size) :
if (size <= 2) :
return
print("\nSIZE : ", size ,"\n\n", end = "")
i = 0
base_support = int(size / 4) + 1
# Print the top layer
while (i < size) :
self.space(size - i + 1)
self.print_symbol(i + 1)
print("\n", end = "")
i += 1
if (base_support <= 1) :
base_support = 2
# Print bottom layers
i = 0
while (i < int(size / 2)) :
self.space(2)
self.print_symbol(base_support)
self.space((size) * 2 - (base_support * 2) * 2)
self.print_symbol(base_support)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.print_hut(5)
obj.print_hut(8)
obj.print_hut(9)
obj.print_hut(13)
if __name__ == "__main__": main()
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
# Ruby Program
# Print Hut Star pattern
class MyPattern
def space(size)
counter = 0
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
def print_symbol(size)
counter = 0
counter = 0
while (counter < size)
print("* ")
counter += 1
end
end
def print_hut(size)
if (size <= 2)
return
end
print("\nSIZE : ", size ,"\n\n")
i = 0
base_support = size / 4 + 1
# Print the top layer
while (i < size)
self.space(size - i + 1)
self.print_symbol(i + 1)
print("\n")
i += 1
end
if (base_support <= 1)
base_support = 2
end
# Print bottom layers
i = 0
while (i < size / 2)
self.space(2)
self.print_symbol(base_support)
self.space((size) * 2 - (base_support * 2) * 2)
self.print_symbol(base_support)
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.print_hut(5)
obj.print_hut(8)
obj.print_hut(9)
obj.print_hut(13)
end
main()
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
/*
Scala Program
Print Hut Star pattern
*/
class MyPattern
{
def space(size: Int): Unit = {
var counter: Int = 0;
counter = 0;
while (counter < size)
{
print(" ");
counter += 1;
}
}
def print_symbol(size: Int): Unit = {
var counter: Int = 0;
counter = 0;
while (counter < size)
{
print("* ");
counter += 1;
}
}
def print_hut(size: Int): Unit = {
if (size <= 2)
{
return;
}
print("\nSIZE : " + size + "\n\n");
var i: Int = 0;
var base_support: Int = (size / 4).toInt + 1;
//Print the top layer
while (i < size)
{
space(size - i + 1);
print_symbol(i + 1);
print("\n");
i += 1;
}
if (base_support <= 1)
{
base_support = 2;
}
//Print bottom layers
i = 0;
while (i < (size / 2).toInt)
{
space(2);
print_symbol(base_support);
space((size) * 2 - (base_support * 2) * 2);
print_symbol(base_support);
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_hut(5);
obj.print_hut(8);
obj.print_hut(9);
obj.print_hut(13);
}
}
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
/*
Swift Program
Print Hut Star pattern
*/
class MyPattern
{
func space(_ size: Int)
{
var counter: Int = 0;
counter = 0;
while (counter < size)
{
print(" ", terminator: "");
counter += 1;
}
}
func print_symbol(_ size: Int)
{
var counter: Int = 0;
counter = 0;
while (counter < size)
{
print("* ", terminator: "");
counter += 1;
}
}
func print_hut(_ size: Int)
{
if (size <= 2)
{
return;
}
print("\nSIZE : ", size ,"\n\n", terminator: "");
var i: Int = 0;
var base_support: Int = size / 4 + 1;
//Print the top layer
while (i < size)
{
self.space(size - i + 1);
self.print_symbol(i + 1);
print("\n", terminator: "");
i += 1;
}
if (base_support <= 1)
{
base_support = 2;
}
//Print bottom layers
i = 0;
while (i < size / 2)
{
self.space(2);
self.print_symbol(base_support);
self.space((size) * 2 - (base_support * 2) * 2);
self.print_symbol(base_support);
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_hut(5);
obj.print_hut(8);
obj.print_hut(9);
obj.print_hut(13);
}
main();
Output
SIZE : 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * * *
SIZE : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
SIZE : 13
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
Time Complexity
The time complexity of this code is O(n), where n is the given size of the hut. The code consists of two loops: one for printing the top layer and the other for printing the bottom layers. Both loops iterate from 0 to n, resulting in a linear time complexity.
Resultant Output Explanation
The resultant output is the hut star pattern printed for different sizes. Each pattern consists of the top layer representing the roof and the bottom layers representing the walls. The output is presented in a formatted manner with asterisks (*) and spaces.
For example, when the size is 5, the top layer has 5 rows, and each row has a different number of asterisks and spaces. The bottom layers have two rows with the base size calculated based on the given size. The hut pattern is printed by combining the required number of spaces, asterisks, and gaps between walls.
Similarly, the patterns for other sizes are printed, showcasing the hut star pattern with different heights and widths.
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