Enter value of A: 10 Enter value of B: 25 Enter value of C: 15
Numbers in Descending Order: 25 15 10
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();
}
}
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).
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.
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.