Signup/Sign In
PUBLISHED ON: APRIL 17, 2021

Java Program to Print Hollow Right Triangle Star Pattern

In this tutorial, we will see how to print the hollow right triangle star pattern in Java First, we will ask the user to initialize the number of rows. Then, we will use different loops to print the hollow right triangle star pattern. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.

Input: Enter the number of rows: 4

Output: The pattern is:

*
* *
* *
* * * *

Program 1: Print the Hollow Right Triangle Star Pattern

In this program, we will see how to print the hollow right triangle star pattern in java using a for loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows.
  4. Ask the user to initialize the variable.
  5. Use a for loop to print the pattern.
  6. Run the outer loop with the structure for(int i=1;i<=n;i++)
  7. The outer for loop iterates from i=1 to n.
  8. If the “if” condition is true, then the first inner loop prints character until j<=i
  9. If the “if” condition is false, then the second inner loop prints character for j=1 or j=i until j<=i, for j!=1 and j!=n prints space.
  10. Display the result.
  11. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program To Print the Hollow Right Triangle Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
        //Create  an instance of the Scanner Class 
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        System.out.println("Enter the symbol: ");
        char ch = sc.next().charAt(0);
	    for(int i=1;i<=n;i++)
        {
	        if(i==1 || i==n)
            for(int j=1;j<=i;j++)                
            {
               System.out.print(ch);
            }
           else
	       {
               for(int j=1;j<=i;j++)
               {  
                  	if(j==1 || j==i)
                   	System.out.print(ch);
                    else
                    System.out.print(" ");
               }
            }
            System.out.println();
        }             
    }
}


Enter the number of rows: 7
Enter the symbol: *
*
**
* *
* *
* *
* *
*******

Program 2: Print the Hollow Right Triangle Star Pattern

In this program, we will see how to print the hollow right triangle star pattern in java using a while loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows.
  4. Ask the user to initialize the variable.
  5. Use a while loop to print the pattern.
  6. Check the condition at outer while for the given “i” value, if it is true.
  7. Then check the “if” condition. If it is true, then initialize j to 1, check the inner while condition j<=i, if true prints character and increases the j value, repeat until condition j<=i is false.
  8. If it is false, then initialize j to 1, checks the while condition, condition true then prints characters for j=1 or j=i otherwise prints space for j!=i and j!=1.
  9. Repeats until the condition at outer while is false.
  10. Display the result.
  11. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program To Print the Hollow Right Triangle Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
        //Create  an instance of the Scanner Class 
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        int i=1;
	    int j;	
	    while(i<=n)
        {
            if(i==1 || i==n)
            {
             	j=1;
             	while(j <=i)
                {
     	            System.out.print("*");
     		        j++;
    	         }
   	         }
             else
	         {
                 j=1;
                 while(j<=i)
                 {
                     if(j==1 || j==i)
                     System.out.print("*");
                     else
                     System.out.print(" ");
		             j++;
                  }
              }
            System.out.println();
            i++;    
        }  
    }
}


Enter the number of rows: 7
*
**
* *
* *
* *
* *
*******

Program 3: Print the Hollow Right Triangle Star Pattern

In this program, we will see how to print the hollow right triangle star pattern in java using a do-while loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows.
  4. Ask the user to initialize the variable.
  5. Use a do-while loop to print the pattern.
  6. Check the if condition.
  7. If it is true, then the first inner do loop prints one character then checks the condition while(++j <=i), if this condition is true then it prints the character, repeats until the condition is false.
  8. If it is false, then the second inner loop prints the character for j=1 or j=i, prints space other than j=1, j=i values. Then check the condition while(++j<=i);, repeats until the condition is false.
  9. Increment the loop variable in each iteration.
  10. Repeat until while(i<=n); is false.
  11. Display the result.
  12. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program To Print the Hollow Right Triangle Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
        //Create  an instance of the Scanner Class 
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        int i=1;
        int j;	
        do 
        {
            if(i==1 || i==n)
            {
             	j=1;
             	do
            	{
     	           System.out.print("*");
     	        }while(++j <=i);
            }
            else
	        {
                j=1;
                do
                {
                    if(j==1 || j==i)
                    System.out.print("*");
                    else
                    System.out.print(" ");
	            }
	            while(++j<=i);
            }
            System.out.println();
            ++i;
        }  while(i<=n);         
    }
}


Enter the number of rows: 8
*
**
* *
* *
* *
* *
* *
********



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.