How to compare 3 number using if and else only | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to compare 3 number using if and else only

Help me compare 3 numbers from highest to lowest using if and else only

14th Sep 2022, 12:10 PM
great Nile
5 Answers
+ 3
Your attempts pls?
14th Sep 2022, 12:16 PM
Jayakrishna 🇮🇳
+ 1
public class Program { public static void main(String[] args) {int num1, num2, num3; num1 = 96; num2 = 420; num3 = 69; if (num1 < num2 && num1 > num3 ) {System.out.println(num2);} else if (num2 > num1 && num2 > num3) {System.out.println(num2);} else {System.out.println(num3);} if (num2 > num3 && num2 >num1) {System.out.println(num1);} else if (num3 > num2 && num3 > num1) {System.out.println(num3);} else {System.out.println(num2);} if (num3 < num1 && num3 > num2) {System.out.println(num3);} else if (num1 < num3 && num1 > num2) {System.out.println(num1);} else {System.out.println(num3);} } }
14th Sep 2022, 3:02 PM
great Nile
+ 1
great Nile In Python: a = int(input()) b = int(input()) c = int(input()) print (a, b, c) if a > b and b > c: print (a) elif b > a and b > c: print (b) else: print (c) You can do same in Java but remember syntax
14th Sep 2022, 3:41 PM
A͢J
A͢J - avatar
+ 1
if (num1 < num2 && num1 > num3 ) these means : num1 is less than num2 and num1 greater than num3, Ie in low to high : num3 < num1 < num2. num1 is in between num3,num2. not greater. similarly others also have incorrect comparisons.. No need to test 3 times. Test num1 greaterness, if false, check num2, if false print num3 Hope it helps... See AJ algarithm...
15th Sep 2022, 2:09 PM
Jayakrishna 🇮🇳
0
Thats my attempt it needs to descend even i change the value to any number
14th Sep 2022, 3:03 PM
great Nile