-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-number-difference
35 lines (28 loc) · 960 Bytes
/
4-number-difference
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
public Integer diff(Integer a, Integer b) {
// APPROACH 1: Using if else statements
// Determine which integer is larger
Integer larger;
Integer smaller;
if (a > b) {
larger = a;
smaller = b;
} else {
larger = b;
smaller = a;
}
// Calculate the absolute difference between the larger and smaller integers
Integer difference = larger - smaller;
// Return the absolute difference
return Math.abs(difference);
// APPROACH 2: Using Math.abs function
//Calculate the absolute difference between a and b
Integer difference = Math.abs(a - b);
// Return the absolute difference
return difference;
}
// Output
diff(5, 2); // 3
diff(2, 5); // 3
///////////////////////////////
// 2.Number Difference: Implement a function diff that calculates the absolute difference between two integers.
//////////////////////////////