Given three numbers A, B and C, write a program to write their values in descending order.

Java Programming Language Decision Making in java (Article) Decision Making in java (Program)

12

Given Input:

Enter value of A: 10
Enter value of B: 25
Enter value of C: 15

Expected Output:

Numbers in Descending Order:
25 15 10

Program:

import java.util.Scanner;

public class DescendingOrder {

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

        System.out.print("Enter value of A: ");
        int A = sc.nextInt();

        System.out.print("Enter value of B: ");
        int B = sc.nextInt();

        System.out.print("Enter value of C: ");
        int C = sc.nextInt();

        int temp;

        // Compare and swap A and B
        if (A < B) {
            temp = A;
            A = B;
            B = temp;
        }

        // Compare and swap A and C
        if (A < C) {
            temp = A;
            A = C;
            C = temp;
        }

        // Compare and swap B and C
        if (B < C) {
            temp = B;
            B = C;
            C = temp;
        }

        System.out.println("Numbers in Descending Order: ");
        System.out.println(A + " " + B + " " + C);

        sc.close();
    }
}

Output:


                                        

Explanation:

How It Works

  • First, compare A and B and swap if needed.

  • Then compare A and C and swap if needed.

  • Finally, compare B and C and swap if needed.

  • After these comparisons, numbers will be arranged in descending order (largest to smallest).


This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.