# The Time in Words Hackerrank problem solution

Given the time in numerals we may convert it into words, as shown below: Write a program which prints the time in words for the input given in the format mentioned above.

Input conditions

There will be two lines of input:
H, representing the hours
M, representing the minutes

Limitation
1<=H<=12
1<=M<=60

Output conditions

Display the time in words.

Input Example

5
47
Output Example

thirteen minutes to six

##### Solution

Important steps to keep in mind.
1. Create lists of word representations for an hour and minute counts. i.e, [one, two,…twelve] and [one, two,…twenty one, twenty two…fifty nine].
2. Handle special cases, such as those which occur when the minutes are 0, 15, 30, 45.
3. If minutes > 30, then compute 60-(minutes) since we will need to report the number of minutes left for the next hour.
4. Take care of the singular case for minute vs. minutes.

C

``````#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int h;
scanf("%d",&h);
int m;
scanf("%d",&m);
char *count[]= {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "quater", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty one", "twenty two", "twenty three", "twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine"};

if(m==0)
printf("%s o' clock",count[h-1]);
else if(m>30){
if(m==59)
printf("%s minute to %s",count[m-1],count[h]);
if(m==45)
printf("quarter to %s",count[h]);
else
printf("%s minutes to %s",count[60-m-1],count[h]);
}
else if(m<30){
if(m==1)
printf("%s minute past %s",count[m-1],count[h-1]);
if(m==15)
printf("quarter past %s",count[h-1]);
else
printf("%s minutes past %s",count[m-1],count[h-1]);
}
else
printf("half past %s",count[h-1]);
return 0;
}
``````

#### Pyhton

``````import sys
words = {1: "one", 2: "two",   3: "three", 4: "four",  5: "five",
6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten",
11: "eleven",   12: "twelve",  13: "thirteen",   14: "fourteen",
15: "fifteen",  16: "sixteen", 17: "seventeen",  18: "eighteen",
19: "nineteen", 20: "twenty",  21: "twenty one", 22: "twenty two",
23: "twenty three", 24: "twenty four",  25: "twenty five",
26: "twenty six",   27: "twenty seven", 28: "twenty eight",
29: "twenty nine"}
h = input()
m = input()
if m == 0:
print words[h], "o' clock"
sys.exit()
elif m == 30:
print "half past", words[h]
if m < 30:
if m == 1:
print "one minute past", words[h]
elif m == 15:
print "quarter past", words[h]
else:
print words[m], "minutes past", words[h]
else:
m = 60 - m
h += 1
if m == 1:
print "one minute to", words[h]
elif m == 15:
print "quarter to", words[h]
else:
print words[m], "minutes to", words[h]``````

#### java

``````import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
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]);
}
}
}``````

#### c++

``````#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string words = {string("one"),string("two"),string("three"),string("four"),string("five"),string("six"),string("seven"),string("eight"),string("nine"),string("ten"),string("eleven"),string("twelve"),string("thirteen"),string("fourteen"),string("fifteen"),string("sixteen"),string("seventeen"),string("eighteen"),string("nineteen"),string("twenty"),string("twenty one"),string("twenty two"),string("twenty three"),string("twenty four"),string("twenty five"),string("twenty six"),string("twenty seven"),string("twenty eight"),string("twenty nine")};

int main() {
int H,M;
cin >> H >> M;
if (M==0) {
printf("%s o' clock\n",words[H-1].c_str());
} else if (M==1) {
printf("one minute past %s\n",words[H-1].c_str());
} else if (M==15) {
printf("quarter past %s\n",words[H-1].c_str());
} else if (M<30) {
printf("%s minutes past %s\n",words[M-1].c_str(),words[H-1].c_str());
} else if (M==30) {
printf("half past %s\n",words[H-1].c_str());
} else if (M==45) {
printf("quarter to %s\n",words[H%12].c_str());
} else if (M==59) {
printf("one minute to %s\n",words[H%12].c_str());
} else {
printf("%s minutes to %s\n",words[60-M-1].c_str(),words[H%12].c_str());
}

return 0;
}
``````

### hasectic

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi