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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

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

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.