Floyd's triangle

Java code to hollow triangle pattern

Java code to hollow triangle pattern

In this tutorial, we will discuss Java code to hollow triangle pattern

We will learn how to create hollow triangle pattern in Java language using nested for loop

Program 1

Code to Hollow triangle pattern 1

Java program to display hollow mirrored right triangle star pattern

import java.util.Scanner;
public class JavaHollowPattern1{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=sc.nextInt();
for(int i=1; i<=rows; i++){//parent for loop lterate rows
  for(int j=i; j<=rows; j++){
     System.out.print(" ");//print space
  }
  for(int j=1; j<=i; j++){
  
  if(i==rows || j==1 || j==i){
     System.out.print("*");//print star
     }
     else{
     System.out.print(" ");
}
}
System.out.println();//move to next line
}
}
}

When the above code is executed, it produces the following results:

Output

 

Program 2

Code to Hollow triangle pattern 2

Java program to display hollow inverted right triangle star pattern

import java.util.Scanner;
public class JavaHollowPattern2{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=sc.nextInt();    //get input from user
for(int i=1; i<=rows; i++){//parent for loop lterate rows
  for(int j=i; j<=rows; j++){
  
  if(i==1 || j==i || j==rows){
     System.out.print("*"); //print star
     }
     else{
     System.out.print(" ");
}

}
System.out.println();//move to next line
}
}
}

When the above code is executed, it produces the following results:

Output

Program 3

Code to Hollow triangle pattern 3

Java program to display hollow inverted mirrored right triangle star pattern

import java.util.Scanner;
public class JavaHollowPattern3{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=sc.nextInt();  //get input from user
for(int i=1; i<=rows; i++){//parent for loop lterate rows
  for(int j=1; j<=i; j++){
     System.out.print(" ");//print space
  }
  for(int j=i; j<=rows; j++){
  
  if(j==i || j==rows || i==1){
     System.out.print("*");
     }
     else{
     System.out.print(" ");
}
}
System.out.println();//move to next line
}
}
}

When the above code is executed, it produces the following results:

Output

 

Program 4

Code to Hollow triangle pattern 4

Java program to display hollow inverted right triangle star pattern

import java.util.Scanner; 
public class JavaHollowPattern{ 
    public static void main(String args[]){ 
        Scanner sc=new Scanner(System.in); 
        System.out.print("Enter the number of rows: "); 
        int rows=sc.nextInt(); 
        //get input from user 
        for(int i=1; i<=rows; i++){
            //parent for loop lterate rows 
            for(int j=1; j<=i; j++){ if(j==1 || j==i || i==rows){ System.out.print("*"); //print star, when the condiiton is true 
            } 
            else{ System.out.print(" "); } } 
            System.out.println();//move to next line 
            } 
            
        } 
        
    }

When the above code is executed, it produces the following results:

hollow triangle pattern

 

 

Similar post

Floyd’s triangle number pattern using for loop in C

Floyd’s triangle pattern using nested for loop in Java

Floyd’s triangle pattern using nested while loop in Java

Hollow pyramid triangle pattern in C++ language

Pyramid star pattern in Cpp

Pyramid star pattern in C

Pyramid star pattern in Java 

 

Suggested for you

Loops in Java language

Loops in C language

Loops in C++ language

 

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

 

While loop in Java language

While loop in C language

While loop in C++ language

While loop in Python language

 

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

 

 

Java code to Floyd's triangle star pattern
Cpp program to hollow triangle star pattern
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago