Knowee
Questions
Features
Study Tools

You have 3 numbers as user input. WAP in Java to output their values in descending order.

Question

You have 3 numbers as user input. WAP in Java to output their values in descending order.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple Java program that takes three numbers as input and outputs them in descending order:

import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        Integer[] numbers = new Integer[3];

        System.out.println("Enter 3 numbers:");
        for (int i = 0; i < 3; i++) {
            numbers[i] = scanner.nextInt();
        }

        Arrays.sort(numbers, Collections.reverseOrder());

        System.out.println("Numbers in descending order:");
        for (int i = 0; i < 3; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Here's how this program works:

  1. We first import the necessary classes from the Java standard library.
  2. We then define a Main class with a main method, which is the entry point of our program.
  3. Inside the main method, we create a Scanner object to read user input.
  4. We also create an array of Integer objects to store the numbers.
  5. We then prompt the user to enter 3 numbers, and we read each number using the nextInt method of our Scanner object, storing them in our array.
  6. We then sort the array in descending order using the sort method of the Arrays class, passing Collections.reverseOrder() as the comparator to sort in reverse order.
  7. Finally, we print the numbers in descending order.

This problem has been solved

Similar Questions

Olivia likes to play with numbers. She told her friends about a game in which Olivia would tell them three numbers and they would have to find the smallest among them. As it was time-consuming, her friends decided to write a program to get three numbers and print the smallest one. Help them by developing an application using your Java skills.Note:Use ternary operator.If all 3 numbers are same then print "All numbers are equal".Negative and zero are also accepted as inputs and the smallest number has to be found accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers587543Sample Output 1:The smallest number is 43Sample Input 2:Enter the numbers8-74Sample Output 2:The smallest number is -7Sample Input 3:Enter the numbers555Sample Output 3:All numbers are equalSample Input 4:Enter the numbers8878Sample Output 4:The smallest number is 8

wap in java to find hcf and lcm of 2 numbers

WAP in Java to accept five subjects marks and calculate the total and average marks.       Example :    Input :     85                            90                           92                           79                           83                   Output : Total Marks : 429  Average Marks : 85.8

WAP to create an array of 6 integer elements use try and catch block to show exceptionoccured when(i)User enter other than integer throw exception and assign rest value to 0.(ii)user enter other than integer throw exception and assign only that value as 0.

WAP that asks the user to enter a number one at a time until the user hits exactly zero (0).Display the sum of all the numbers that were entered by the user before hitting zero.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.