Write a Java program to left shift the square pattern of odd numbers using a for loop.
package Shapes4; import java.util.Scanner; public class SquareLeftShiftOddNum1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square Left Shift Odd Numbers Rows = "); int rows = sc.nextInt(); System.out.println("Left Shift the Square Pattern of Odd Numbers"); for (int i = 1; i <= rows; i++) { for (int j = i - 1; j < rows; j++ ) { System.out.print(j * 2 + 1 + " "); } for(int k = 0; k < i - 1; k++) { System.out.print(k * 2 + 1 + " "); } System.out.println(); } } }
It is another way of writing the Java program to print the square pattern of odd numbers left shifted from top to bottom.
package Shapes4; import java.util.Scanner; public class SquareLeftShiftOddNum2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square Left Shift Odd Numbers Rows = "); int rows = sc.nextInt(); System.out.println("Left Shift the Square Pattern of Odd Numbers"); for (int i = 1; i <= rows; i++) { int j = (i * 2) - 1; for(int k = 1; k <= rows; k++) { System.out.print(j + " "); j += 2; if(j > (rows * 2) - 1) { j = 1; } } System.out.println(); } } }
Enter Square Left Shift Odd Numbers Rows = 13
Left Shift the Square Pattern of Odd Numbers
1 3 5 7 9 11 13 15 17 19 21 23 25
3 5 7 9 11 13 15 17 19 21 23 25 1
5 7 9 11 13 15 17 19 21 23 25 1 3
7 9 11 13 15 17 19 21 23 25 1 3 5
9 11 13 15 17 19 21 23 25 1 3 5 7
11 13 15 17 19 21 23 25 1 3 5 7 9
13 15 17 19 21 23 25 1 3 5 7 9 11
15 17 19 21 23 25 1 3 5 7 9 11 13
17 19 21 23 25 1 3 5 7 9 11 13 15
19 21 23 25 1 3 5 7 9 11 13 15 17
21 23 25 1 3 5 7 9 11 13 15 17 19
23 25 1 3 5 7 9 11 13 15 17 19 21
25 1 3 5 7 9 11 13 15 17 19 21 23
This Java example displays the square pattern of odd numbers that are left shifted using a while loop.
package Shapes4; import java.util.Scanner; public class SquareLeftShiftOddNum3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square Left Shift Odd Numbers Rows = "); int rows = sc.nextInt(); System.out.println("Left Shift the Square Pattern of Odd Numbers"); int i, j, k; i = 1; while(i <= rows) { j = i - 1; while(j < rows) { System.out.print(j * 2 + 1 + " "); j++; } k = 0; while(k < i - 1) { System.out.print(k * 2 + 1 + " "); k++; } System.out.println(); i++; } } }
Enter Square Left Shift Odd Numbers Rows = 15
Left Shift the Square Pattern of Odd Numbers
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
3 5 7 9 11 13 15 17 19 21 23 25 27 29 1
5 7 9 11 13 15 17 19 21 23 25 27 29 1 3
7 9 11 13 15 17 19 21 23 25 27 29 1 3 5
9 11 13 15 17 19 21 23 25 27 29 1 3 5 7
11 13 15 17 19 21 23 25 27 29 1 3 5 7 9
13 15 17 19 21 23 25 27 29 1 3 5 7 9 11
15 17 19 21 23 25 27 29 1 3 5 7 9 11 13
17 19 21 23 25 27 29 1 3 5 7 9 11 13 15
19 21 23 25 27 29 1 3 5 7 9 11 13 15 17
21 23 25 27 29 1 3 5 7 9 11 13 15 17 19
23 25 27 29 1 3 5 7 9 11 13 15 17 19 21
25 27 29 1 3 5 7 9 11 13 15 17 19 21 23
27 29 1 3 5 7 9 11 13 15 17 19 21 23 25
29 1 3 5 7 9 11 13 15 17 19 21 23 25 27