Java Code to Compare Three Variables
Java Programming Language Decision Making in java (Article) Decision Making in java (Program)
17
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:
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);
}
}
}
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:
-
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.
-
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.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
The greatest number is: 20
Program:
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); } } }
Output:
The greatest number is: 20
Explanation:
- We have three variables:
num1
,num2
, andnum3
. - The
if-else
block compares the values:- If
num1
is greater than or equal to bothnum2
andnum3
, then it is the greatest. - Otherwise, if
num2
is greater than or equal to bothnum1
andnum3
, thennum2
is the greatest. - If neither of the above is true, then
num3
is the greatest.
- If
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:
-
Check for equality: Before comparing the values, the code first checks if all three variables (
num1
,num2
, andnum3
) are equal using the conditionif (num1 == num2 && num2 == num3)
. If this condition is true, it prints a message indicating that all the numbers are equal. -
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.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.