Home / Programs / Java Code to Compare Three Variables
🚀 Programming Example

Java Code to Compare Three Variables

👁 110 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 Information & Algorithm

To compare three variables in Java and find which one is the greatest, you can use simple if-else conditions. Below is a Java code example that demonstrates how to compare three variables and print the greatest value:

Given Input:

        int num1 = 10;
        int num2 = 20;
        int num3 = 15;

Expected Output:

The greatest number is: 20

💻 Program Code

public class CompareThreeNumbers {
    public static void main(String[] args) {
        // Define three variables
        int num1 = 10;
        int num2 = 20;
        int num3 = 15;

        // Logic to find the greatest number
        if (num1 >= num2 && num1 >= num3) {
            System.out.println("The greatest number is: " + num1);
        } else if (num2 >= num1 && num2 >= num3) {
            System.out.println("The greatest number is: " + num2);
        } else {
            System.out.println("The greatest number is: " + num3);
        }
    }
}

                        

🖥 Program Output

The greatest number is: 20

                            

📘 Explanation

  • We have three variables: num1, num2, and num3.
  • The if-else block compares the values:
    • If num1 is greater than or equal to both num2 and num3, then it is the greatest.
    • Otherwise, if num2 is greater than or equal to both num1 and num3, then num2 is the greatest.
    • If neither of the above is true, then num3 is the greatest.

Important Note:

This logic works even if some of the variables have equal values. For example, if num1, num2, and num3 are all equal, the program will still return one of the values as the greatest (since they are all the same).

To handle the case where all three variables are equal, you can add additional checks to specifically detect when the values are equal. Here's an updated version of the code that accounts for this condition and prints an appropriate message if all the values are the same:


public class CompareThreeNumbers {
    public static void main(String[] args) {
        // Define three variables
        int num1 = 20;
        int num2 = 20;
        int num3 = 20;

        // Check if all three numbers are equal
        if (num1 == num2 && num2 == num3) {
            System.out.println("All numbers are equal: " + num1);
        }
        // Logic to find the greatest number if they are not all equal
        else if (num1 >= num2 && num1 >= num3) {
            System.out.println("The greatest number is: " + num1);
        } else if (num2 >= num1 && num2 >= num3) {
            System.out.println("The greatest number is: " + num2);
        } else {
            System.out.println("The greatest number is: " + num3);
        }
    }
}

Explanation of Changes:

  1. Check for equality: Before comparing the values, the code first checks if all three variables (num1, num2, and num3) are equal using the condition if (num1 == num2 && num2 == num3). If this condition is true, it prints a message indicating that all the numbers are equal.

  2. Handling the greatest number: If the values are not all equal, the program proceeds with the regular comparison logic to determine the greatest value.

This solution ensures that when all the variables have the same value, a message indicating that they are equal will be displayed, and the logic for finding the greatest value will only run when the numbers are different.

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