Write a program in Java to store 30 numbers in an array then display those numbers in descending order with a condition that maximum 5 numbers will appear in one line.
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] array = new int[11];
for (int i = 0; i < array.length; i++) {
array[i] = in.nextInt();
}
Arrays.sort(array);
for (int i = array.length - 1, j = 1; i >= 0; i--, j++) {
System.out.print(array[i] + " ");
if (j % 5 == 0) {
System.out.println();
}
}
}
}
Comments
Leave a comment