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 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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.