Write a Java program to print square of left shift numbers pattern using a for loop.
package Shapes4; import java.util.Scanner; public class SquareLeftShiftNum1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square Left Shift Numbers Rows = "); int rows = sc.nextInt(); System.out.println("The Square Pattern of Left Shift Numbers"); for (int i = 1; i <= rows; i++) { for (int j = i; j <= rows; j++ ) { System.out.print(j + " "); } for(int k = 1; k < i; k++) { System.out.print(k + " "); } System.out.println(); } } }
It is a different way of writing the Java program to print the square pattern of left shift numbers.
package Shapes4; import java.util.Scanner; public class SquareLeftShiftNum2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square Left Shift Numbers Rows = "); int rows = sc.nextInt(); System.out.println("The Square Pattern of Left Shift Numbers"); for (int i = 1; i <= rows; i++) { int j = i; for(int k = 1; k <= rows; k++) { System.out.print(j + " "); j++; if(j > rows) { j = 1; } } System.out.println(); } } }
Enter Square Left Shift Numbers Rows = 16
The Square Pattern of Left Shift Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1
3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2
4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3
5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4
6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5
7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6
8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7
9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8
10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10
12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11
13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12
14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13
15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14
16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This Java example displays the square pattern of left shifted numbers from top to bottom using a while loop.
package Shapes4; import java.util.Scanner; public class SquareLeftShiftNum3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square Left Shift Numbers Rows = "); int rows = sc.nextInt(); System.out.println("The Square Pattern of Left Shift Numbers"); int i, j, k; i = 1; while(i <= rows) { j = i; while(j <= rows) { System.out.print(j + " "); j++; } k = 1; while(k < i) { System.out.print(k + " "); k++; } System.out.println(); i++; } } }
Enter Square Left Shift Numbers Rows = 14
The Square Pattern of Left Shift Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14
2 3 4 5 6 7 8 9 10 11 12 13 14 1
3 4 5 6 7 8 9 10 11 12 13 14 1 2
4 5 6 7 8 9 10 11 12 13 14 1 2 3
5 6 7 8 9 10 11 12 13 14 1 2 3 4
6 7 8 9 10 11 12 13 14 1 2 3 4 5
7 8 9 10 11 12 13 14 1 2 3 4 5 6
8 9 10 11 12 13 14 1 2 3 4 5 6 7
9 10 11 12 13 14 1 2 3 4 5 6 7 8
10 11 12 13 14 1 2 3 4 5 6 7 8 9
11 12 13 14 1 2 3 4 5 6 7 8 9 10
12 13 14 1 2 3 4 5 6 7 8 9 10 11
13 14 1 2 3 4 5 6 7 8 9 10 11 12
14 1 2 3 4 5 6 7 8 9 10 11 12 13