-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose10
More file actions
27 lines (26 loc) · 826 Bytes
/
close10
File metadata and controls
27 lines (26 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*Given 2 int values, return whichever value is nearest to the value 10, or return 0 in the event of a tie.
Note that Math.abs(n) returns the absolute value of a number.
close10(8, 13) → 8
close10(13, 8) → 8
close10(13, 7) → 0
*/
public class close10 {
public static void main(String[] args) {
int output=close10(8,13);
System.out.println(output);
int output1=close10(13,8);
System.out.println(output1);
int output2=close10(13,7);
System.out.println(output2);
int output3=close10(11,11);
System.out.println(output3);
}
public static int close10(int a, int b){
if (Math.abs(a-10) < Math.abs(b-10))
return a;
else if (Math.abs(a-10) > Math.abs(b-10))
return b;
else
return 0;
}
}