Java Program to Print Hut Number Pattern

Program to Print Hut Number Pattern

In the previous article, we have discussed Java Program to Print Heart Number Pattern

In this article we are going to see how to print the Hut number pattern.

        0
      111
    22222
  3333333
444444444
555      555
666      666
777      777

Approach to Print Hut Number Pattern

  • 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 to Print Hut Number Pattern

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(i);
                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(i);
            }
            System.out.println("");
        }
    }
 }
}

Output: 0 111 22222 3333333 444444444 555 555 666 666 777 777

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs: