Write a Java program to print right arrow number pattern using for loop.
import java.util.Scanner; public class RightArrowNumber1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j; System.out.print("Enter Right Arrow Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Arrow Numbers Pattern"); for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= rows; j++ ) { if(j < i) { System.out.print(" "); } else { System.out.print(j); } } System.out.println(); } for (i = 1 ; i < rows; i++ ) { for (j = 1 ; j <= rows; j++ ) { if(j < rows - i) { System.out.print(" "); } else { System.out.print(j); } } System.out.println(); } } }
This is another way of of writing a Java program to display the right arrow pattern of numbers using a while loop.
import java.util.Scanner; public class RightArrowNumber2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j, k; System.out.print("Enter Right Arrow Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Arrow Numbers Pattern"); for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j < i; j++ ) { System.out.print(" "); } for (k = i ; k <= rows; k++ ) { System.out.print(k); } System.out.println(); } for (i = rows - 1 ; i >= 1; i-- ) { for (j = 1 ; j < i; j++ ) { System.out.print(" "); } for (k = i ; k <= rows; k++ ) { System.out.print(k); } System.out.println(); } } }
Enter Right Arrow Number Pattern Rows = 9
Printing Right Arrow Numbers Pattern
123456789
23456789
3456789
456789
56789
6789
789
89
9
89
789
6789
56789
456789
3456789
23456789
123456789