w3resource

Java Exercises: Accepts six numbers as input and sorts them in descending order

Java Basic: Exercise-221 with Solution

Write a Java program that accepts six numbers as input and sorts them in descending order.

Input:

Input consists of six numbers n1, n2, n3, n4, n5, n6 (-100000 ≤ n1, n2, n3, n4, n5, n6 ≤ 100000). The six numbers are separated by a space.

Pictorial Presentation:

Java Basic Exercises: Accepts six numbers as input and sorts them in descending order.

Sample Solution:

Java Code:

 import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
 
public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input six integers:");
        String[] input = br.readLine().split(" ", 6);
        int[] data = new int[6];
 
        for (int i = 0; i < 6; i++) {
            data[i] = Integer.parseInt(input[i]);
        }
 
        for (int j = 0; j < 5; j++) {
            for (int i = 5; i > j; i--) {
                if (data[i - 1] < data[i]) {
                    int swp = data[i];
                    data[i] = data[i - 1];
                    data[i - 1] = swp;
                }
            }
        }
        StringBuilder sb = new StringBuilder(); 
        for (int i : data) {
            sb.append(i);
            sb.append(" ");
        }
        System.out.println("After sorting the said integers:");
        System.out.println(sb.substring(0 , sb.length()-1));
    }
}

Sample Output:

Input six integers:
 4 6 8 2 7 9
After sorting the said integers:
9 8 7 6 4 2

Flowchart:

Flowchart: Java exercises: Accepts six numbers as input and sorts them in descending order.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to compute and print sum of two given integers (more than or equal to zero). If given integers or the sum have more than 80 digits, print "overflow".
Next: Write a Java program to test whether two lines PQ and RS are parallel. The four points are P(x1, y1), Q(x2, y2), R(x3, y3), S(x4, y4).

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH