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
Java program to display the right triangle star pattern
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:
Enter the number of rows: 9 * ** *** **** ***** ****** ******* ******** ********* **********
Pattern 2
Java program to display mirrored right triangle star pattern
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:
Enter the number of rows: 9 * ** *** **** ***** ****** ******* ******** *********
Pattern 3
Java program to display reverse right triangle star pattern
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:
Enter the number of rows: 9 ********* ******** ******* ****** ***** **** *** ** *
Pattern 4
Program to display reverse mirrored right triangle star pattern
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:
Enter te number of rows: 9 ********* ******** ******* ****** ***** **** *** ** *
Suggested for you
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language
Floyd’s triangle number pattern using for loop in C
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,…