# The Time in Words – HackerRank Solution

In this post, we will solve The Time in Words HackerRank Solution. This problem (The Time in Words) is a part of HackerRank Problem Solving series.

Contents

Given the time in numerals we may convert it into words, as shown below:

5:00 -> five o’ clock
5:01 -> one minute past five
5:10 -> ten minute past five
5:15 -> quarter past five
5:30 -> half past five
5:40 -> twenty minutes to six
5:45 -> quarter to six
5:47 -> thirteen minutes to six
5:28 -> twenty eight minutes past five

AtÂ minutes = 0, useÂ o’ clock. ForÂ 1 <= minutes <= 30, useÂ past, and forÂ 30 < minutesÂ useÂ to. Note the space between the apostrophe andÂ clockÂ inÂ o’ clock. Write a program which prints the time in words for the input given in the format described.

Function Description

Complete the timeInWords function in the editor below.

timeInWords has the following parameter(s):

• int h: the hour of the day
• int m: the minutes after the hour

Returns

• string:Â a time string as described

## Input Format

The first line containsÂ h, the hours portion The second line containsÂ m, the minutes portion

## Constraints

• 1 <= h <= 12
• 0 <= m < 60

Sample Input 0

``````5
47``````

Sample Output 0

``thirteen minutes to six``

Sample Input 1

``````3
00``````

Sample Output 1

``three o' clock``

Sample Input 2

``````7
15``````

Sample Output 2

``quarter past seven``

## Solution – The Time in Words – HackerRank Solution

### C++

```#include <iostream>
using namespace std;

string s[21] = {"",        "one",     "two",       "three",    "four",
"five",    "six",     "seven",     "eight",    "nine",
"ten",     "eleven",  "twelve",    "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty"};

int main() {
int h, m;
cin >> h >> m;
if (m == 0) {
cout << s[h] << " o' clock" << endl;
} else if (m < 15) {
cout << s[m] << " minute" << (m == 1 ? "" : "s") << " past " << s[h]
<< endl;
} else if (m == 15) {
cout << "quarter past " << s[h] << endl;
} else if (m <= 20) {
cout << s[m] << " minutes past " << s[h] << endl;
} else if (m < 30) {
cout << "twenty " << s[m - 20] << " minutes past " << s[h] << endl;
} else if (m == 30) {
cout << "half past " << s[h] << endl;
} else if (m < 40) {
cout << "twenty " << s[40 - m] << " minutes to " << s[h + 1] << endl;
} else if (m < 45) {
cout << s[60 - m] << " minutes to " << s[h + 1] << endl;
} else if (m == 45) {
cout << "quarter to " << s[h + 1] << endl;
} else {
cout << s[60 - m] << " minute" << (m == 59 ? "" : "s") << " to " << s[h + 1]
<< endl;
}
return 0;
}
```

### Python

```import sys

def timeInWords(h, m):
res = ''
numbers = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve', 'thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty']
minute = 'minute'

if m != 1:
minute += 's'

if m == 0:
res = numbers[h] + " o' clock"
elif m == 30:
res = "half past " + numbers[h]
elif m == 15:
res = "quarter past " + numbers[h]
elif m == 45:
res = "quarter to " + numbers[h + 1]
elif m < 20:
res = numbers[m] + ' ' + minute + ' past ' + numbers[h]
elif m < 30:
res = numbers[-1] + ' ' + numbers[int(m%10)] + ' ' + minute + ' past ' + numbers[h]
elif m > 45:
res = numbers[60 - m] + ' ' + minute + ' to ' + numbers[h + 1]
elif m > 30:
res = numbers[-1] + ' ' + numbers[int(m%10)] + ' ' + minute + ' to ' + numbers[h + 1]

return res.replace('  ', ' ')

if __name__ == "__main__":
h = int(input().strip())
m = int(input().strip())
result = timeInWords(h, m)
print(result)
```

### Java

```import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int h = in .nextInt();
int m = in .nextInt();
String word[] = {
"",
"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"
};
String time = "";

if (m == 0) {
time = word[h] + " o' clock";
} else if (m == 15) {
time = "quarter past " + word[h];
} else if (m == 30) {
time = "half past " + word[h];
} else if (m == 45) {
time = "quarter to " + word[h + 1];
} else if (m == 1) {
time = word[m] + " minute past " + word[h];
} else if (m < 30) {
time = word[m] + " minutes past " + word[h];
} else {
time = word[60 - m] + " minutes to " + word[h + 1];
}
System.out.println(time);
}
}
```

Note: This problem (The Time in Words) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.