Display Hut Star Pattern
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
*
***
*****
*******
*********
***********
*************
*** ***
*** ***
*** ***
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