In this tutorial, we will discuss Java code to Floyd’s triangle star pattern using nested for loop.
We will learn how to create Floyd’s triangle pattern in Java programming language using for loop
Pattern 1
Floyd’s triangle pattern 1
Java Code for Floyd’s triangle program 1
Java program to display the right triangle star pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class JavaStarPattern{
public static voidmain(String args[]){
Scanner sc=newScanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=sc.nextInt();//get input from user
for(int i=0; i<=rows; i++){
for(int j=0; j<=i; j++){
System.out.print("*");//print star
}
System.out.println();
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
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=0; i<=rows; i++){
for(int j=0; j<=i; j++){
System.out.print("*");//print star
}
System.out.println();
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
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=0; i<=rows; i++){
for(int j=0; j<=i; j++){
System.out.print("*");//print star
}
System.out.println();
}
}
}
When the above code is executed, it produces the following results:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the number of rows: 9
*
**
***
****
*****
******
*******
********
*********
**********
Enter the number of rows: 9
*
**
***
****
*****
******
*******
********
*********
**********
Enter the number of rows: 9
*
**
***
****
*****
******
*******
********
*********
**********
Pattern 2
Floyd’s triangle pattern 2
Java Code for Floyd’s triangle program 2
Java program to display mirrored right triangle star pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class JavaStarPattern{
public static voidmain(String args[]){
Scanner sc=newScanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=sc.nextInt();
for(int i=1; i<=rows; i++){//parent for loop for print rows
for(int j=1; j<=rows-i; j++){
System.out.print(" ");//print space
}
for(int k=1; k<=i; k++){
System.out.print("*");//print star
}
System.out.println();//move to next line
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
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 for print rows
for(int j=1; j<=rows-i; j++){
System.out.print(" ");//print space
}
for(int k=1; k<=i; k++){
System.out.print("*");//print star
}
System.out.println();//move to next line
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
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 for print rows
for(int j=1; j<=rows-i; j++){
System.out.print(" ");//print space
}
for(int k=1; k<=i; k++){
System.out.print("*");//print star
}
System.out.println();//move to next line
}
}
}
When the above code is executed, it produces the following results:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the number of rows: 9
*
**
***
****
*****
******
*******
********
*********
Enter the number of rows: 9
*
**
***
****
*****
******
*******
********
*********
Enter the number of rows: 9
*
**
***
****
*****
******
*******
********
*********
Pattern 3
Floyd’s triangle pattern 3
Java Code for Floyd’s triangle program 3
Java program to display reverse right triangle star pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class JavaStarPattern{
public static voidmain(String args[]){
Scanner sc=newScanner(System.in);
System.out.print("Enter the number of rows\n: ");
int rows=sc.nextInt();//get input from user
for(int i=rows; i>=1; i--){
for(int j=1; j<=i; j++){
System.out.print("*"); /print star
}
System.out.println();
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows\n: ");
int rows=sc.nextInt();//get input from user
for(int i=rows; i>=1; i--){
for(int j=1; j<=i; j++){
System.out.print("*"); /print star
}
System.out.println();
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows\n: ");
int rows=sc.nextInt();//get input from user
for(int i=rows; i>=1; i--){
for(int j=1; j<=i; j++){
System.out.print("*"); /print star
}
System.out.println();
}
}
}
When the above code is executed, it produces the following results:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the number of rows: 9
*********
********
*******
******
*****
****
***
**
*
Enter the number of rows: 9
*********
********
*******
******
*****
****
***
**
*
Enter the number of rows: 9
*********
********
*******
******
*****
****
***
**
*
Pattern 4
Floyd’s triangle pattern 4
Code for Floyd’s triangle program 4
Program to display reverse mirrored right triangle star pattern
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class JavaStarPattern{
public static voidmain(String args[]){
Scanner sc=newScanner(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=1; j<=i; j++){
System.out.print(" ");//print space
}
for(int k=i; k<=rows; k++){
System.out.print("*");//print star
}
System.out.println();//move to next line
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
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=1; j<=i; j++){
System.out.print(" ");//print space
}
for(int k=i; k<=rows; k++){
System.out.print("*");//print star
}
System.out.println();//move to next line
}
}
}
import java.util.Scanner;
public class JavaStarPattern{
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=1; j<=i; j++){
System.out.print(" ");//print space
}
for(int k=i; k<=rows; k++){
System.out.print("*");//print star
}
System.out.println();//move to next line
}
}
}
When the above code is executed, it produces the following results:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter te number of rows: 9
*********
********
*******
******
*****
****
***
**
*
Enter te number of rows: 9
*********
********
*******
******
*****
****
***
**
*
Enter te number of rows: 9
*********
********
*******
******
*****
****
***
**
*