SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Time in Words ISC Practical 2003 Program in Java and Python

18 October 2013

ISC Practical 2003 time in words program with algorithm, explanation, Java solution and simple Python solution.

Question:

Given a time in numbers, we can convert it into words. For example:

5 : 00 ----- five o'clock 5 : 10 ----- ten minutes 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

Write a program which first inputs two integers, the first between 1 and 12, both inclusive, and the second between 0 and 59, both inclusive. The program should then print out the time they represent in words. Your program should follow the format of the examples above.

Sample Data: 1. INPUT: TIME: 3,0 OUTPUT: 3 : 00 Three o'clock 2. INPUT: TIME: 7,29 OUTPUT: 7 : 29 Twenty nine minutes past seven 3. INPUT: TIME: 6,34 OUTPUT: 6 : 34 Twenty six minutes to seven 4. INPUT: TIME: 12,1 OUTPUT: 12 : 01 One minute past Twelve 5. INPUT: TIME: 12,45 OUTPUT: 12 : 45 Quarter to One 6. INPUT: TIME: 10,59 OUTPUT: 10 : 59 One minute to Eleven 7. INPUT: TIME: 14,60 OUTPUT: Incorrect Input

Test your program for the data values given in the examples above and some random data.

Algorithm:

Step 1: Start.

Step 2: Accept the hour and minute values from the user.

Step 3: If the hour is not from 1 to 12 or the minute is not from 0 to 59, display Incorrect Input and go to Step 15.

Step 4: Store number names from 1 to 29 in a string array.

Step 5: If the minute is 1 or 59, store minute; otherwise store minutes.

Step 6: Find the next hour in words. If the current hour is 12, the next hour is One; otherwise it is the word at position hour + 1.

Step 7: Convert the numeric minute into two-digit form for display.

Step 8: If minute is 0, print the hour followed by o'clock.

Step 9: If minute is 15, print Quarter past followed by the current hour.

Step 10: If minute is 30, print Half past followed by the current hour.

Step 11: If minute is 45, print Quarter to followed by the next hour.

Step 12: If minute is less than 30, print the minute in words followed by past and the current hour.

Step 13: If minute is greater than 30, print 60 - minute in words followed by to and the next hour.

Step 14: Display the final time in the required format.

Step 15: Stop.

Explanation:

This program converts a numeric time into the form usually spoken in English. The problem is not solved by spelling the complete time digit by digit. Instead, the minute value decides the phrase pattern. For example, 10 minutes after an hour is expressed using past, while 20 minutes before the next hour is expressed using to. Therefore the program first validates the input and then separates the logic into exact cases such as 0, 15, 30 and 45 minutes, and general cases before and after the half-hour mark.

The array words stores number names from 1 to 29. This range is enough because the largest minute value printed directly is 29, as in Twenty nine minutes past seven. For times after 30 minutes, the program does not print the actual minute value. It calculates how many minutes are left for the next hour using 60 - minute. For example, 6:34 is not printed as thirty four minutes past six. It is printed as twenty six minutes to seven, so the required word is found from words[26].

Special cases are handled before the general conditions. If the minute is 0, the phrase is simply the hour followed by o'clock. If the minute is 15 or 45, the word Quarter is used. If the minute is 30, the phrase is Half past. These phrases are standard expressions and should not be generated using ordinary minute words. The program also chooses between minute and minutes so that 12:01 becomes One minute past Twelve and 10:59 becomes One minute to Eleven.

The next-hour calculation is important when the phrase uses to. For 8:45, the output should refer to nine, not eight. However, if the hour is 12, adding 1 directly would give 13, which is invalid in a 12-hour clock. So the program stores One as the next hour when the input hour is 12. The minute is also formatted with a leading zero when needed, so values such as 3:0 and 12:1 are displayed as 3 : 00 and 12 : 01 in the final answer.

Java Program:

Java
/**
* The class TimeInWords_ISC2003 inputs hours and minutes and prints
* the time represented by them in words.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2003, Question 2
*/

import java.util.Scanner;

public class TimeInWords_ISC2003
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Hours: ");
        int hour = sc.nextInt();

        System.out.print("Enter Minutes: ");
        int minute = sc.nextInt();

        if((hour >= 1 && hour <= 12) && (minute >= 0 && minute <= 59))
        {
            /*
            * The array stores the words required for hours and minutes.
            * Index 0 is kept blank so that words[1] gives One.
            */
            String words[] = {"", "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 minuteWord;

            // Use singular form only for one minute past or one minute to.
            if(minute == 1 || minute == 59)
                minuteWord = "minute";
            else
                minuteWord = "minutes";

            String nextHour;

            // After 12, the next hour in a 12-hour clock is 1.
            if(hour == 12)
                nextHour = words[1];
            else
                nextHour = words[hour + 1];

            String displayMinute;

            // Display minutes in two digits, such as 00, 01 and 09.
            if(minute < 10)
                displayMinute = "0" + minute;
            else
                displayMinute = "" + minute;

            System.out.print("Output: " + hour + " : " + displayMinute + " ");

            if(minute == 0)
                System.out.println(words[hour] + " o'clock");
            else if(minute == 15)
                System.out.println("Quarter past " + words[hour]);
            else if(minute == 30)
                System.out.println("Half past " + words[hour]);
            else if(minute == 45)
                System.out.println("Quarter to " + nextHour);
            else if(minute < 30)
                System.out.println(words[minute] + " " + minuteWord + " past " + words[hour]);
            else
                System.out.println(words[60 - minute] + " " + minuteWord + " to " + nextHour);
        }
        else
        {
            System.out.println("Incorrect Input");
        }
    }
}

Equivalent Python Program:

Python
hour = int(input("Enter Hours: "))
minute = int(input("Enter Minutes: "))

if hour >= 1 and hour <= 12 and minute >= 0 and minute <= 59:
    # Index 0 is blank so that words[1] gives One.
    words = ["", "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"]

    # Singular form is needed only for one minute past or one minute to.
    if minute == 1 or minute == 59:
        minute_word = "minute"
    else:
        minute_word = "minutes"

    # In a 12-hour clock, the hour after 12 is 1.
    if hour == 12:
        next_hour = words[1]
    else:
        next_hour = words[hour + 1]

    # Format minutes as 00, 01, 02 and so on.
    if minute < 10:
        display_minute = "0" + str(minute)
    else:
        display_minute = str(minute)

    print("Output:", hour, ":", display_minute, end=" ")

    if minute == 0:
        print(words[hour] + " o'clock")
    elif minute == 15:
        print("Quarter past", words[hour])
    elif minute == 30:
        print("Half past", words[hour])
    elif minute == 45:
        print("Quarter to", next_hour)
    elif minute < 30:
        print(words[minute], minute_word, "past", words[hour])
    else:
        print(words[60 - minute], minute_word, "to", next_hour)
else:
    print("Incorrect Input")

Output:

Example 1: Enter Hours: 12 Enter Minutes: 39 Output: 12 : 39 Twenty one minutes to One Example 2: Enter Hours: 7 Enter Minutes: 16 Output: 7 : 16 Sixteen minutes past Seven Example 3: Enter Hours: 5 Enter Minutes: 30 Output: 5 : 30 Half past Five Example 4: Enter Hours: 8 Enter Minutes: 45 Output: 8 : 45 Quarter to Nine Example 5: Enter Hours: 2 Enter Minutes: 60 Incorrect Input Example 6: Enter Hours: 4 Enter Minutes: 0 Output: 4 : 00 Four o'clock

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →