-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTwoNumbers.java
More file actions
50 lines (40 loc) · 1.15 KB
/
Copy pathHTwoNumbers.java
File metadata and controls
50 lines (40 loc) · 1.15 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.Scanner;
public class HTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
double x = (double) a / b;
// Floor
int floor;
if (x >= 0 || x == (int) x)
floor = (int) x;
else
floor = (int) x - 1;
// Ceil
int ceil;
if (x <= 0 || x == (int) x)
ceil = (int) x;
else
ceil = (int) x + 1;
// Round (Half Up)
int round;
int integer = (int) x;
double fraction = x - integer;
if (x >= 0) {
if (fraction >= 0.5)
round = integer + 1;
else
round = integer;
} else {
if (fraction <= -0.5)
round = integer - 1;
else
round = integer;
}
System.out.println("floor " + a + " / " + b + " = " + floor);
System.out.println("ceil " + a + " / " + b + " = " + ceil);
System.out.println("round " + a + " / " + b + " = " + round);
sc.close();
}
}