Home / Programs / Given three numbers A, B and C, write a program to write their values in descending order.
Programming Example

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

👁 15 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

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 Code

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

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).

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.