Java Code To Reverse Order Triangle Number Patterns
In this tutorial, we will discuss Java code to reverse order triangle number patterns. We will see some reverse Floyd’s triangle number patterns, using nested for loop.
Floyd’s triangle number pattern
Pattern program 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want? "); int rows=sc.nextInt(); System.out.println(""); for (int i=1; i<=rows; i++){ for (int j=i; j>=1; j--){ System.out.print(j); } System.out.println(); } } } |
When the above code is executed, it produces the following results:
1 2 3 4 5 6 7 8 9 10 | Enter the rows you want? 6 1 21 321 4321 54321 654321 |
In this Floyd’s triangle pattern, numbers are arranged to reverse order in every line – used nested for loop
Pattern program 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the number of rows you want "); int rows=sc.nextInt(); System.out.println(""); while(rows>=1){ int j=rows; while(j>=1){ System.out.print(j); j--; } rows--; System.out.println(); } } } |
When the above code is executed, it produces the following results:
1 2 3 4 5 6 7 8 9 10 11 | Enter the number of rows you want 7 7654321 654321 54321 4321 321 21 1 |
Pattern program 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want :"); int rows=sc.nextInt(); System.out.println(""); while(rows>0){ for (int i=1; i<=rows; i++){ System.out.print(rows); } System.out.println(" "); rows--; } } } |
When the above code is executed, it produces the following results:
1 2 3 4 5 6 7 8 9 10 11 | Enter the rows you want :7 7777777 666666 55555 4444 333 22 1 |