Java Program to Print Hollow Right Triangle Star Pattern


In this article, we will understand how to print hollow right triangle star pattern. The pattern is formed by using multiple for-loops and print statements. For the pyramid at the first line it will print one star, and at the last line it will print n number of stars. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the size : 8

Output

The desired output would be −

The hollow pyramid triangle :
*
* *
*  *
*   *
*    *
*     *
********

Algorithm

Step 1 - START
Step 2 - Declare four integer values namely i, j, k and my_input.
Step 3 - Read the required values from the user/ define the values
Step 4 - We iterate through two nested 'for' loops to get space between the characters.
Step 5 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character.
Step 6 - Now, print a newline to get the specific number of characters in the subsequent lines.
Step 7 - Display the result
Step 8 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool run button.

import java.util.Scanner;
public class Hollowtangle{
   public static void main(String[] args){
      int my_input, k, i, j;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the size : ");
      my_input = my_scanner.nextInt();
      for( i=1;i<=my_input;i++){
         if(i==1 || i==my_input)
         for( j=1;j<=i;j++){
            System.out.print("*");
         } else {
             for( j=1;j<=i;j++){
                if(j==1 || j==i)
                   System.out.print("*");
                else
                   System.out.print(" ");
            }
         }
      System.out.println();
      }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the size : 8
**
* *
*  *
*   *
*    *
*     *
********

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Hollowtangle{
   public static void main(String[] args){
      int my_input, i, j;
      my_input = 8;
      System.out.println("The size is defined as " +my_input);
      for( i=1;i<=my_input;i++){
         if(i==1 || i==my_input)
         for( j=1;j<=i;j++){
            System.out.print("*");
         } else {
            for( j=1;j<=i;j++){
               if(j==1 || j==i)
                  System.out.print("*");
               else
                  System.out.print(" ");
            }
         }
         System.out.println();
      }
   }
}

Output

The size is defined as 8
*
**
* *
*  *
*   *
*    *
*     *
********

Updated on: 25-Feb-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements