-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathConvertTimeInWords.java
More file actions
76 lines (66 loc) · 1.62 KB
/
ConvertTimeInWords.java
File metadata and controls
76 lines (66 loc) · 1.62 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class MyClass {
public static void main(String[] args) {
//This program will take numerical inputs for hour and minutes, after that
//based on these values it will convert the numerical time to time in words.
//Example: Sample input : 06:15 --> Sample output : quatrter past six
String[] numberWords = new String[] {
"",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
"twenty",
"twenty one",
"twenty two",
"twenty three",
"twenty four",
"twenty five",
"twenty six",
"twenty seven",
"twenty eight",
"twenty nine"
};
Scanner in = new Scanner(System.in);
int hour = in.nextInt();
int minute = in.nextInt();
int nextHour = (hour % 12) + 1;
if(minute == 0) {
System.out.printf("%s o' clock\n", numberWords[hour]);
}
else if(minute == 15) {
System.out.printf("quarter past %s\n", numberWords[hour]);
}
else if(minute == 30) {
System.out.printf("half past %s\n", numberWords[hour]);
}
else if(minute == 45) {
System.out.printf("quarter to %s\n", numberWords[nextHour]);
}
else if(minute < 30) {
System.out.printf("%s minutes past %s\n", numberWords[minute], numberWords[hour]);
}
else {
System.out.printf("%s minutes to %s\n", numberWords[60 - minute], numberWords[nextHour]);
}
}
}