Print Hut Star pattern
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
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
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