In this tutorial, we will discuss the Hollow Triangle star Pattern in Java using nested while loop
In this program, we are going to learn about how to display Hollow Tringle star pattern using nested while loop in Java programming language
Here, we display some Hollow triangle Pattern program with coding using nested while loop and also program get input from the user using Scanner class in Java programming language
The user can provide numbers as they wish and get the Hollow Triangle pattern according to their input.
Program 1
import java.util.Scanner;//import the scanner class class Triangle_While1{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object System.out.print("Enter the number of rows"); int rows=scan.nextInt(); int i,j; i=1; while(i<=rows){ j=1; while(j<=i){ System.out.print(" "); if(j==1 || j==i ||i==rows ) System.out.print("*"); else System.out.print(" "); j++;; } i++; System.out.print("\n");//Move to the next line for print } } }
When the above code is executed, it produces the following results
Program 2
import java.util.Scanner;//import the scanner class class Triangle_While2{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object System.out.print("Enter the number of rows: "); int rows=scan.nextInt(); int i,j; i=1; while(i<=rows){ j=i; while(j<rows){ System.out.print(" "); j++; } j=1; while(j<=i){ if(j==i || j==1 ||i==rows ) System.out.print("*"); else System.out.print(" "); j++;; } i++; System.out.print("\n");//Move to the next line for print } } }
When the above code is executed, it produces the following results
Program 3
import java.util.Scanner;//import the scanner class class Triangle_While3{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object System.out.print("Enter the number of rows: "); int rows=scan.nextInt(); int i,j; i=1; while(i<=rows){ j=1; while(j<=rows){ if(j==i || j==rows ||i==1 ) System.out.print("*"); else System.out.print(" "); j++;; } i++; System.out.print("\n");//Move to the next line for print } } }
When the above code is executed, it produces the following results
Program 4
import java.util.Scanner;//import the scanner class class Triangle_While4{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object System.out.print("Enter the number of rows: "); int rows=scan.nextInt(); int i,j; i=1; while(i<=rows){ j=i; while(j<=rows){ if( i==1|| j==i || j==rows) System.out.print("*"); else System.out.print(" "); j++;; } i++; System.out.print("\n");//Move to the next line for print } } }
When the above code is executed, it produces the following results
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…