Skip to content
TheCScience
TheCScience
  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
TheCScience
HackerRank The Time in Words Problem Solution

HackerRank The Time in Words Problem Solution

Yashwant Parihar, April 18, 2023April 19, 2023

In this post, we will solve HackerRank The Time in Words Problem Solution.

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 minutes past five
5:15→ quarter past five
5:30half 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.

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
HackerRank The Time in Words Problem Solution
HackerRank The Time in Words Problem Solution

The Time in Words C Solution

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

int main() {
	
	char string[31][13];
	
	strncpy (string[0], "o' clock ", 13);
	strncpy (string[1], "one ", 13);
    strncpy (string[2], "two ", 13);
    strncpy (string[3], "three ", 13);
    strncpy (string[4], "four ", 13);
    strncpy (string[5], "five ", 13);
    strncpy (string[6], "six ", 13);
    strncpy (string[7], "seven ", 13);
    strncpy (string[8], "eight ", 13);
    strncpy (string[9], "nine ", 13);
    strncpy (string[10], "ten ", 13);
    strncpy (string[11], "eleven ", 13);
    strncpy (string[12], "twelve ", 13);
    strncpy (string[13], "thirteen ", 13);
    strncpy (string[14], "fourteen ", 13);
    strncpy (string[15], "quarter ", 13);
    strncpy (string[16], "sixteen ", 13);
    strncpy (string[17], "seventeen ", 13);
    strncpy (string[18], "eighteen ", 13);
    strncpy (string[19], "nineteen ", 13);
    strncpy (string[20], "twenty ", 13);
    strncpy (string[21], "twenty one ", 13);
    strncpy (string[22], "twenty two ", 13);
    strncpy (string[23], "twenty three ", 13);
    strncpy (string[24], "twenty four ", 13);
    strncpy (string[25], "twenty five ", 13);
    strncpy (string[26], "twenty six ", 13);
    strncpy (string[27], "twenty seven ", 13);
    strncpy (string[28], "twenty eight ", 13);
    strncpy (string[29], "twenty nine ", 13);
    strncpy (string[30], "half ", 13);
    
    int H, M;
    scanf("%d", &H);
    scanf("%d", &M);
    
    if (M == 0) {
		printf("%s", string[H]);
		printf("%s", string[M]);
    }
    else if (M == 1) {
    	printf("%sminute past ", string[M]);
		printf("%s", string[H]);
	}
    else if ((M > 1) && (M < 14)) {
    	printf("%sminutes past ", string[M]);
		printf("%s", string[H]);
	}
	else if ((M > 15) && (M < 30)) {
    	printf("%sminutes past ", string[M]);
		printf("%s", string[H]);
	}
	else if (M == 15) {
    	printf("%spast ", string[M]);
		printf("%s", string[H]);
	}
	else if (M == 30) {
    	printf("%spast ", string[M]);
		printf("%s", string[H]);
	}
	else if ((M > 30) && (M < 44)) {
    	printf("%sminutes to ", string[60 - M]);
		printf("%s", string[H + 1]);
	}
	else if ((M > 45) && (M < 59)) {
    	printf("%sminutes to ", string[60 - M]);
		printf("%s", string[H + 1]);
	}
	else if (M == 45) {
    	printf("%sto ", string[60 - M]);
		printf("%s", string[H + 1]);
	}
	else if (M == 59) {
    	printf("%sminute ", string[60 - M]);
		printf("%s", string[H + 1]);
	}
	
    return 0;
}

The Time in Words C++ Solution

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


int main() {
  int h, m;
  cin >> h >> m;

  map<int, string> num { {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"} };

  switch(m) {
  case 0:
    cout << num[h] << " o' clock";
    break;
  case 1: 
    cout << num[m] << " minute past " << num[h];
    break;
  case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
    cout << num[m] << " minutes past " << num[h];
    break;
  case 11: case 12: case 13: case 14: case 16: case 17: case 18: case 19: case 20:
    cout << num[m] << " minutes past " << num[h];
    break;
  case 15:
    cout << "quarter past " << num[h];
    break;
  case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29:
    cout << "twenty " << num[m-20] << " minutes past " << num[h];
    break;
  case 30:
    cout << "half past " << num[h];
    break;
  case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39:
    cout << "twenty " << num[40-m] << " minutes to " << num[h+1];
    break;
  case 40:
    cout << "twenty minutes to " << num[h+1];
    break;
  case 45:
    cout << "quarter to " << num[h+1];
    break;
  case 41: case 42: case 43: case 44: case 46: case 47: case 48: case 49:
  case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59:
    cout << num[60-m] << " minutes to " << num[h+1];
    break;
  default: 
    cout << "TODO";

  }

  return 0;
}

The Time in Words C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    
    static string numberToWords(int n){
        switch (n)
        {
            case 1:
            return "one";
            break;
            case 2:
            return "two";
            break;
            case 3:
            return "three";
            break;
            case 4:
            return "four";
            break;
            case 5:
            return "five";
            break;
            case 6:
            return "six";
            break;
            case 7:
            return "seven";
            break;
            case 8:
            return "eight";
            break;
            case 9:
            return "nine";
            break;
            case 10:
            return "ten";
            break;
            case 11:
            return "eleven";
            break;
            case 12:
            return "twelve";
            break;
            case 13:
            return "thirteen";
            break;
            case 14:
            return "fourteen";
            break;
            case 15:
            return "quarter";
            break;
            case 16:
            return "sixteen";
            break;
            case 17:
            return "seventeen";
            break;
            case 18:
            return "eighteen";
            break;
            case 19:
            return "nineteen";
            break;
            case 20:
            return "twenty";
            break;
            case 21:
            return "twenty one";
            break;
            case 22:
            return "twenty two";
            break;
            case 23:
            return "twenty three";
            break;
            case 24:
            return "twenty four";
            break;
            case 25:
            return "twenty five";
            break;
            case 26:
            return "twenty six";
            break;
            case 27:
            return "twenty seven";
            break;
            case 28:
            return "twenty eight";
            break;
            case 29:
            return "twenty nine";
            break;
            default:
            return "zero";
            break;
        }
    }
    static void Main(String[] args) {
        int hour = Convert.ToInt32(Console.ReadLine());
        int minute = Convert.ToInt32(Console.ReadLine());
        if (minute == 0){
            Console.WriteLine(numberToWords(hour)+" o' clock");
        } else if (minute == 1){
            Console.WriteLine("one minute past "+numberToWords(hour));
        } else if (minute == 15){
            Console.WriteLine("quarter past "+numberToWords(hour));
        } else if (minute < 30){
            Console.WriteLine(numberToWords(minute)+" minutes past "+numberToWords(hour));
        } else if (minute == 30){
            Console.WriteLine("half past "+numberToWords(hour));
        } else if (minute == 45){
            Console.WriteLine("quarter to "+numberToWords(hour+1));
        } else {
            Console.WriteLine(numberToWords(60 - minute)+" minutes to "+ numberToWords(hour+1));
        }
        //TODO (1 minute (without s) to hour), (quarter past)?
    }
}

The Time in Words Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'timeInWords' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts following parameters:
     *  1. INTEGER h
     *  2. INTEGER m
     */

    public static class Number{
        String str;
        int num;
        public Number(String s, int n) {
            this.str = s;
            this.num = n;
   }
    }
    public static String timeInWords(int h, int m) {
    // Write your code here
    Number options[] = new Number[20];
    Number one = new Number("one", 1);
    options[0] = one;
    Number two = new Number("two", 2);
    options[1] = two;
    Number three = new Number("three", 3);
    options[2] = three;
    Number four = new Number("four", 4);
    options[3] = four;
    Number five = new Number("five", 5);
    options[4] = five;
    Number six = new Number("six", 6);
    options[5] = six;
    Number seven = new Number("seven", 7);
    options[6] = seven;
    Number eight = new Number("eight", 8);
    options[7] = eight;
    Number nine = new Number("nine", 9);
    options[8] = nine;
    Number ten = new Number("ten", 10);
    options[9] = ten;
    Number eleven = new Number("eleven", 11);
    options[10] = eleven;
    Number twelve = new Number("twelve", 12);
    options[11] = twelve;
    Number thirteen = new Number("thirteen", 13);
    options[12] = thirteen;
    Number fourteen = new Number("fourteen", 14);
    options[13] = fourteen;
    Number fifteen = new Number("fifteen", 15);
    options[14] = fifteen;
    Number sixteen = new Number("sixteen", 16);
    options[15] = sixteen;
    Number seventeen = new Number("seventeen", 17);
    options[16] = seventeen;
    Number eighteen = new Number("eighteen", 18);
    options[17] = eighteen;
    Number nineteen = new Number("nineteen", 19);
    options[18] = nineteen;
    Number twenty = new Number("twenty", 20);
    options[19] = twenty;
    
    
    
    
    if(m < 30){
        String hour = " ";
        String minute = " ";
        for(int i = 0; i < 12; i++){
            if(options[i].num == h){
                hour = options[i].str;
            }
        }
        if(m == 15){
            return ("quarter past " + hour);
        }
        if(m == 0){
            return(hour + " o' clock");
        }
        if(m == 20){
            return("twenty minutes past " + hour);
        }
        if(m > 20){
            int holder = m -20;
            for(int i = 0; i < 9; i++){
                if(options[i].num == holder){
                    return("twenty " + options[i].str + " minutes past " + hour);
                }
            }
        }
        for(int i = 0; i < 18; i++){
            if(options[i].num == m){
                minute = options[i].str;
            }
        }
        if(minute.equals("one")){
            return(minute + " minute past " + hour);
        }
        return(minute + " minutes past " + hour);
    } else if(m == 30){
        String hour = " ";
        for(int i = 0; i < 12; i++){
            if(options[i].num == h){
                hour = options[i].str;
            }
        }
        return("half past " + hour);
        
        
        
        }else {
        String hour = " ";
        String minute = " ";
        for(int i = 0; i < 12; i++){
            if(options[i].num == h){
                hour = options[i + 1].str;
                if(i == 11){
                    hour = "one";
                }
            }
        }
        if(m == 45){
            return("quarter to " + hour);
        }
        if(m == 40){
            return("twenty minutes to " + hour);
        }
        int min = 60 - m;
       if(min > 20){
           int holder = min -20;
            for(int i = 0; i < 9; i++){
                if(options[i].num == holder){
                    return("twenty " + options[i].str + " minutes to " + hour);
                }
            }
        }
        for(int i = 0; i < 18; i++){
            if(options[i].num == min){
                minute = options[i].str;
            }
        }
        if(minute.equals("one")){
            return(minute + " minute to " + hour);
        }
        return(minute + " minutes to " + hour);
    }
   // return("end");
        
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int h = Integer.parseInt(bufferedReader.readLine().trim());

        int m = Integer.parseInt(bufferedReader.readLine().trim());

        String result = Result.timeInWords(h, m);

        bufferedWriter.write(result);
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

The Time in Words JavaScript Solution

function processData(input) {
    //Enter your code here
    var nums = input.split('\n');
    var hour = parseInt(nums[0]);
    var minute = parseInt(nums[1]);
    var desc = '';
    var s = '';
    
    if(minute > 1) {
        s = 's';
    }
    
    var sminute = numberToText(minute);
        
    if(minute == 0) {
        desc+= an[hour] + ' o\' clock';
    }else if(minute == 15) {    
        desc+= 'quarter past '+ an[hour];
    } else if(minute > 0 && minute < 30) {
        desc+= sminute + ' minute'+s+' past '+ an[hour];
    } else if(minute == 30  ){
        desc+= 'half past '+ an[hour];
    } else if (minute == 45) {
        desc+= 'quarter to '+ an[hour+1];
    } else if(minute > 30) {
        minute = 60-minute;
        if(minute > 1) {
            s = 's';
        }
        sminute = numberToText(minute);
        desc+= sminute + ' minute'+s+' to '+ an[hour+1];
    }
    
    console.log(desc);
} 

function numberToText(minute) {
    var sminute = '';
    var p = minute.toString().split('');
    
    if(minute < 10 || ( minute > 10 && minute < 20 )){
        sminute = an[minute]
    } else if(minute % 10 == 0){
        sminute = am[parseInt(p[0])];
    } else if(minute > 20) {
        sminute = am[parseInt(p[0])];
        sminute += ' '+an[parseInt(p[1])];
    }
    return sminute;
}



var an = {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:'eigthteen',
          19:'nineteen'
         };
var am = {1:'ten',2:'twenty', 3: 'thirty', 4:'forty',5:'fifty',6:'sixty',7:'seventy',8:'eighty',9:'ninety'};



process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

The Time in Words Python Solution

import os
import sys

def getTime( hrs, mins ):
	numList = [	'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'ninteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six', 'twenty seven', 'twenty eight', 'twenty nine' ]
	if mins > 30:
		hour = numList[ hrs + 1 ]
	else:
		hour = numList[ hrs ]

	if mins == 0:
		time = hour + ' o\' clock'

	elif mins == 15:
		time = 'quarter past ' + hour

	elif mins < 30:
		time = numList[ mins ] + ' minutes past ' + hour

	elif mins == 30:
		time = 'half past ' + hour

	elif mins != 45:
		time = numList[ 60 - mins ] + ' minutes to ' + hour

	else:
		time = 'quarter to ' + hour

	print( time );

def main():
	hours = int( input() )
	minutes = int ( input() )
	getTime( hours, minutes )

if __name__ == "__main__":
	main()

other Solution

  • HackerRank Chocolate Feast Problem Solution
  • HackerRank Service Lane Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 TheCScience | WordPress Theme by SuperbThemes