Write a C program to left shift the square pattern of odd numbers using a for loop.
#include <stdio.h> int main() { int rows; printf("Enter Square Left Shift Odd Numbers Rows = "); scanf("%d", &rows); printf("Left Shift the Square Pattern of Odd Numbers\n"); for (int i = 1; i <= rows; i++) { for (int j = i - 1; j < rows; j++) { printf("%d ", j * 2 + 1); } for (int k = 0; k < i - 1; k++) { printf("%d ", k * 2 + 1); } printf("\n"); } }
It is another way of writing the C program to print the square pattern of odd numbers that are left shifted from top to bottom.
#include <stdio.h> int main() { int rows; printf("Enter Square Left Shift Odd Numbers Rows = "); scanf("%d", &rows); printf("Left Shift the Square Pattern of Odd Numbers\n"); for (int i = 1; i <= rows; i++) { int j = (i * 2) - 1; for (int k = 1; k <= rows; k++) { printf("%d ", j); j += 2; if (j > (rows * 2) - 1) { j = 1; } } printf("\n"); } }
Enter Square Left Shift Odd Numbers Rows = 12
Left Shift the Square Pattern of Odd Numbers
1 3 5 7 9 11 13 15 17 19 21 23
3 5 7 9 11 13 15 17 19 21 23 1
5 7 9 11 13 15 17 19 21 23 1 3
7 9 11 13 15 17 19 21 23 1 3 5
9 11 13 15 17 19 21 23 1 3 5 7
11 13 15 17 19 21 23 1 3 5 7 9
13 15 17 19 21 23 1 3 5 7 9 11
15 17 19 21 23 1 3 5 7 9 11 13
17 19 21 23 1 3 5 7 9 11 13 15
19 21 23 1 3 5 7 9 11 13 15 17
21 23 1 3 5 7 9 11 13 15 17 19
23 1 3 5 7 9 11 13 15 17 19 21
This C example displays the square pattern of odd numbers that are left shifted using a while loop.
#include <stdio.h> int main() { int rows, i, j, k; printf("Enter Square Left Shift Odd Numbers Rows = "); scanf("%d", &rows); printf("Left Shift the Square Pattern of Odd Numbers\n"); i = 1; while (i <= rows) { j = i - 1; while (j < rows) { printf("%d ", j * 2 + 1); j++; } k = 0; while (k < i - 1) { printf("%d ", k * 2 + 1); k++; } printf("\n"); 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