Java Program to Print Hollow Right Angled Triangle Star Pattern

Write a Java Program to print the hollow right angled triangle star pattern using for loop. This Java hollow right triangle star example uses if condition inside nested for loop to find the outside rows and display the hollow right triangle.

package ShapePrograms;

import java.util.Scanner;

public class HollowRightTriangle1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Right Angled Triangle Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Hollow Right Angled Triangle Star Pattern");
		
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= i; j++ ) 
			{
				if(i == 1 || i == rows || j == 1 || j == i)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print Hollow Right Angled Triangle Star Pattern 1

In this Java hollow right angled triangle star pattern program, we replaced for loops with while loops.

package ShapePrograms;

import java.util.Scanner;

public class HollowRightTriangle2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Right Angled Triangle Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Hollow Right Angled Triangle Star Pattern");
		int i = 1, j ;
		
		while( i <= rows) 
		{
			j = 1 ;
			while( j <= i ) 
			{
				if(i == 1 || i == rows || j == 1 || j == i)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}
				j++;
			}
			System.out.println();
			i++ ;
		}
	}
}
Enter Hollow Right Angled Triangle Pattern Rows = 11
Hollow Right Angled Triangle Star Pattern
*
**
* *
*  *
*   *
*    *
*     *
*      *
*       *
*        *
***********

Java Program to Print Hollow Right Angled Triangle Star Pattern using do while loop

package ShapePrograms;

import java.util.Scanner;

public class HollowRightTriangle3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Right Angled Triangle Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Hollow Right Angled Triangle Star Pattern");
		int i = 1, j ;
		
		do
		{
			j = 1 ;
			do
			{
				if(i == 1 || i == rows || j == 1 || j == i)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}
			} while( ++j <= i );
			System.out.println();
		} while( ++i <= rows) ;
	}
}
Enter Hollow Right Angled Triangle Pattern Rows = 14
Hollow Right Angled Triangle Star Pattern
*
**
* *
*  *
*   *
*    *
*     *
*      *
*       *
*        *
*         *
*          *
*           *
**************

In this Java example, the HollowRightTrianglePat function prints a given symbol’s hollow right angled triangle pattern.

package ShapePrograms;

import java.util.Scanner;

public class HollowRightTriangle4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Right Angled Triangle Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Character for Hollow Right Angled Triangle Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Hollow Right Angled Triangle Pattern");
		HollowRightTrianglePat(rows, ch);
	}
	
	public static void HollowRightTrianglePat(int rows, char ch) {
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= i; j++ ) 
			{
				if(i == 1 || i == rows || j == 1 || j == i)
				{
					System.out.print(ch);
				}
				else
				{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}
Enter Hollow Right Angled Triangle Pattern Rows = 18
Character for Hollow Right Angled Triangle Pattern = #
Hollow Right Angled Triangle Pattern
#
##
# #
#  #
#   #
#    #
#     #
#      #
#       #
#        #
#         #
#          #
#           #
#            #
#             #
#              #
#               #
##################