Write a Java program to print inverted triangle numbers pattern using for loop.
package Shapes3; import java.util.Scanner; public class InvertedTriangleNum1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Inverted Triangle Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Inverted Triangle Number Pattern"); for (int i = 1; i <= rows; i++ ) { for (int j = 1; j < i; j++ ) { System.out.print(" "); } for(int k = 1; k <= rows - i + 1; k++) { System.out.print(k + " "); } System.out.println(); } } }
This Java program prints the inverted triangle pattern of numbers using a while loop.
package Shapes3; import java.util.Scanner; public class InvertedTriangleNum2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Inverted Triangle Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Inverted Triangle Number Pattern"); int j, k, i = 1; while(i <= rows ) { j = 1; while (j < i ) { System.out.print(" "); j++; } k = 1; while(k <= rows - i + 1) { System.out.print(k + " "); k++; } System.out.println(); i++; } } }
Enter Inverted Triangle Number Pattern Rows = 5
Printing Inverted Triangle Number Pattern
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
This Java example displays the inverted triangle pattern of numbers using the do while loop.
package Shapes3; import java.util.Scanner; public class InvertedTriangleNum3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Inverted Triangle Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Inverted Triangle Number Pattern"); int j, k, i = 1; do { j = 1; do { System.out.print(" "); } while (j++ < i ); k = 1; do { System.out.print(k + " "); } while(++k <= rows - i + 1); System.out.println(); } while(++i <= rows ) ; } }
Enter Inverted Triangle Number Pattern Rows = 9
Printing Inverted Triangle Number Pattern
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1