Task
In three-digit number find which digit is bigger — the most right or the most left. If all digits are equal, enter as an result the word «equals».
Similar task to this can be found here.
Tests
Try to find all possible combinations distributed by growing. Should be not less than 13 combinations.
Input | Output |
555 | equals |
554 | 5 |
545 | equals |
455 | 5 |
557 | 7 |
575 | equals |
755 | 7 |
321 | 3 |
312 | 3 |
231 | 3 |
213 | 3 |
123 | 3 |
132 | 3 |
Solution
With «if» statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.*; import java.lang.*; import java.io.*; class Main{ public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int x = s.nextInt(); int y = x / 100; int z = x % 10; if(y > z){ System.out.println(y); } else if(y < z ){ System.out.println(z); } else{ System.out.println("equals"); } s.close(); } } |
Code refactoring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.*; import java.lang.*; import java.io.*; class Main{ public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int x = s.nextInt(); int y = x / 100; int z = x % 10; System.out.println(y > z? y : y < z? z : "equals"); s.close(); } } |