
please explain the logic to program the following using for loop in Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7

Here is a code that displays the following pattern:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
public class Pattern {
public static void main(String[] args) {
int[] array = {1,3,5,7,9};
System.out.println("Original array is:");
printArray(array);
for(int i=1;i<=4;i++){
shift(array, 1);
printArray(array);
}
}
public static void shift(int[] array, int amount) {
for (int j = 0; j < amount; j++) {
int a = array[0];
int i;
for (i = 0; i < array.length - 1; i++)
array[i] = array[i + 1];
array[i] = a;
}
}
public static void printArray(int[] array) {
for (int x = 0; x < array.length; x++) {
System.out.print(array[x] + " ");
}
System.out.println();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.