-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose10.java
More file actions
36 lines (33 loc) · 981 Bytes
/
Copy pathclose10.java
File metadata and controls
36 lines (33 loc) · 981 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
28
29
30
31
32
33
34
35
36
/*
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.
*/
package codingbat;
import java.util.*;
public class close10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1, num2;
System.out.println("Enter two numbers:");
num1 = sc.nextInt();
num2 = sc.nextInt();
System.out.println("Result: "+close(num1, num2));
}
public static int close(int num1,int num2){
int num1_diff, num2_diff;
num1_diff = Math.abs(10 - num1);
num2_diff = Math.abs(10 - num2);
if(num1_diff != num2_diff){
if(num1_diff > num2_diff){
return num2;
}
else{
return num1;
}
}
else{
return 0;
}
}
}