Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Conditional Constructs in Java

Chapter 8

Conditional Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Display Three Numbers in Descending Order


20. Write a program in Java to read three integers and display them in descending order.

import java.util.Scanner;

 

public class DescendingOrder {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        // Input three numbers

        System.out.print("Enter first number: ");

        int a = sc.nextInt();

 

        System.out.print("Enter second number: ");

        int b = sc.nextInt();

 

        System.out.print("Enter third number: ");

        int c = sc.nextInt();

 

        // Sorting in descending order using comparisons

        int largest, middle, smallest;

 

        if (a >= b && a >= c) {

            largest = a;

            if (b >= c) {

                middle = b;

                smallest = c;

            } else {

                middle = c;

                smallest = b;

            }

        } else if (b >= a && b >= c) {

            largest = b;

            if (a >= c) {

                middle = a;

                smallest = c;

            } else {

                middle = c;

                smallest = a;

            }

        } else {

            largest = c;

            if (a >= b) {

                middle = a;

                smallest = b;

            } else {

                middle = b;

                smallest = a;

            }

        }

 

        // Output

        System.out.println("Numbers in descending order:");

        System.out.println(largest + " " + middle + " " + smallest);

 

        sc.close();

    }

}

Output

Sample Input 
Enter first number: 45
Enter second number: 12
Enter third number: 78

Sample Output 
Numbers in descending order:
78 45 12

Explanation

  • The program reads three integers using the Scanner class.
  • Conditional statements (if-else) are used to:
    • Find the largest, middle, and smallest numbers.
  • The numbers are then printed in descending order (largest → smallest).