Q:

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

0

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.

Expected Output:

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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));
    }
}

Output:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now