by CodeChum Admin
Did you know that you can also reverse lists and group them according to your liking with proper code? Let's try it to believe it.
Instructions:
Input
The first line contains an odd positive integer.
The next lines contains an integer.
5
1
3
4
5
2
Output
A line containing a list without space.
[2,5]-[4]-[3,1]
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter an interger");
int n=input.nextInt();
int [] array = new int [] { 1, 3, 4, 5,2};
System.out.println("Original array: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("Array in reverse order: ");
for (int i = array.length-1; i >= 0; i--) {
System.out.print("["+array[i] + "] ");
}
}
}
Comments
Leave a comment