Program to Print Hut Star Pattern
In this article we are going to see how to print the Hut star pattern.
* *** ***** ******* ********* *** *** *** *** *** ***
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Approach:
- Enter total row and store it in an integer variable say row.
- Take a inner loop to print the column values.
- Take another nested loop to print the upper triangle i.e. Roof.
- Take another nested for loop to print the lower part i.e. Walls.
JAVA Code:
Methdo-1 : Static Star Character
public class Main
{
public static void main(String[] args)
{
int i, c, space, row = 8, symb = 0;
// Print upper triangle- ROOF
for (i = 0; i < row; i++)
{
if (i < 5)
{
// print space in upper triangle
for (space = 1; space < 5 - i; space++)
{
System.out.print(" ");
}
// print symbol after printing spaces
while (symb != (2 * i + 1))
{
System.out.print("*");
symb++;;
}
//re-initialize symb to 0
symb = 0;
// move to next line/row
System.out.println("");
}
// print WALLS
else {
for (c = 0; c < 9; c++)
{
// typecast float to int type
if ((int) (c / 3) == 1)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println("");
}
}
}
}
Output: * *** ***** ******* ********* *** *** *** *** *** ***
Method-2 : User Input Character
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int i, c, space, row = 8, symb = 0;
Scanner sc=new Scanner(System.in);
// Enter any random character
System.out.print("Enetr any character : ");
char s=sc.next().charAt(0);
// Print upper triangle- ROOF
for (i = 0; i < row; i++)
{
if (i < 5)
{
// print space in upper triangle
for (space = 1; space < 5 - i; space++)
{
System.out.print(" ");
}
// print symbol after printing spaces
while (symb != (2 * i + 1))
{
System.out.print(s);
symb++;;
}
//re-initialize symb to 0
symb = 0;
// move to next line/row
System.out.println("");
}
// print WALLS
else {
for (c = 0; c < 9; c++)
{
// typecast float to int type
if ((int) (c / 3) == 1)
System.out.print(" ");
else
System.out.print(s);
}
System.out.println("");
}
}
}
}
Output: Enter any character : @ @ @@@ @@@@@ @@@@@@@ @@@@@@@@@ @@@ @@@ @@@ @@@ @@@ @@@
C Code:
# include<stdio.h>
int main()
{
int r, c, space, row = 8, symb = 0;
for (r = 0; r < row; r++)
{
if (r < 5) {
for (space = 1; space < 5 - r; space++)
{
printf(" ");
}
while (symb != (2 * r + 1))
{
printf("*");
symb++;;
}
symb = 0;
printf("\n");
} else
{
for (c = 0; c < 9; c++)
{
if ((int) (c / 3) == 1)
printf(" ");
else
printf("*");
}
printf("\n");
}
}
return 0;
}
Output: * *** ***** ******* ********* *** *** *** *** *** ***
C++ Code:
# include <iostream>
using namespace std;
int main() {
int r, c, space, row = 8, symb = 0;
for (r = 0; r < row; r++) {
if (r < 5)
{
for (space = 1; space < 5 - r; space++) {
cout<<" ";
}
while (symb != (2 * r + 1)) {
cout<<"*";
symb++;;
}
symb = 0;
cout<<("\n");
}
else
{
for (c = 0; c < 9; c++)
{
if ((int) (c / 3) == 1)
cout<<" ";
else
cout<<"*";
}
cout<<("\n");
}
}
return 0;
}
Output: * *** ***** ******* ********* *** *** *** *** *** ***
Related Java Star Pattern Programs: