VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-given-time-words/

⇱ Convert given time into words - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert given time into words

Last Updated : 16 Feb, 2023

Given a time in the format of hh:mm (12-hour format) 0 < hh < 12, 0 <= mm < 60. The task is to convert it into words as shown:
Examples : 
 

Input : h = 5, m = 0
Output : five o' clock

Input : h = 6, m = 24
Output : twenty four minutes past six


Corner cases are m = 0, m = 15, m = 30 and m = 45.
 

6:00 six o'clock
6:10 ten minutes past six
6:15 quarter past six
6:30 half past six
6:45 quarter to seven
6:47 thirteen minutes to seven


 


The idea is to use the if-else-if statement to determine the time in words. According to the above-given example, on the basis of minutes, we can categorize time in words into 8, which are minutes equal to 0, 15, 30, 45, 1, 59, and in a range less than 30 or greater than 30. Check the value of minutes and print accordingly.
Below is the implementation of this approach:
 


Output
twenty four minutes past six

Time Complexity: O(1)

Auxiliary Space: O(1) as constant space has been used


 

Comment