Thursday , 27 July 2017
Latest Posts
Home » ISC Important Programs » Program to print a given Time in Words [ISC Practical 2003]

# Program to print a given Time in Words [ISC Practical 2003]

#### 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 second between 0 and 59 (both inclusive) and then prints out the time they represent, in words.

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.

#### Programming Code:

```/**
* The class TimeInWords_ISC2003 inputs hours & minutes and then prints out the time they represent, in words
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2003, Question 2
*/

import java.io.*;
public class TimeInWords_ISC2003
{
public static void main(String args[])throws IOException
{

/* Inputting hours and minutes */
System.out.print("Enter Hours : ");
System.out.print("Enter Minutes : ");

if((h>=1 && h<=12) && (m>=0 && m<=59)) // checking whether given input is legal or not.
{
/* creating an array containing numbers from 1-29 in words */

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"};

/* The below code is for finding whether to print the word 'minute' or 'minutes' */
String plu, a;
if(m == 1 || m == 59)
plu = "Minute";
else
plu = "Minutes";

/* When we have minutes from 31-59, we print the hour ahead of the given hour
* like 6:55 will be 5 minutes to 7 and not 5 minutes to 6
* when we print the hour ahead of the given hour, we face a problem at hour = 12
* because if we print an hour ahead of 12, it will be thirteen, but we want 1
* so the below code checks this & decides what hour to print in words when minutes is from 31-59
*/
if(h==12)
a = words[1]; //storing 'one' when hour is 12
else
a = words[h+1]; //if hour is not 12, then storing in words, an hour ahead of given hour

/* The below code checks minutes and accordingly prints the time in words using array. */
System.out.print("Output : "+h+":"+m+" ----- "); //printing the given time in numbers

if(m==0)
System.out.println(words[h]+" O' clock");
else if(m==15)
System.out.println("Quarter past "+words[h]);
else if(m==30)
System.out.println("Half past "+words[h]);
else if(m==45)
System.out.println("Quarter to "+a);
else if(m<30) // condition for minutes between 1-29
System.out.println(words[m]+" "+plu+" past "+words[h]);
else // condition for minutes between 31-59
System.out.println(words[60-m]+" "+plu+" to "+a);
} //end of outer if

else
System.out.println("Invalid Input !"); //printing error message for illegal input
}
}```

#### Output:

Example 1:
Enter Hours : 12
Enter Minutes : 39
Output : 12:39 —– Twenty one Minutes to One

Example 2:
Enter Hours : 12
Enter Minutes : 39
Output : 12:39 —– Twenty one Minutes to One

Example 3:
Enter Hours : 7
Enter Minutes : 16
Output : 7:16 —– Sixteen Minutes past Seven

Example 4:
Enter Hours : 5
Enter Minutes : 30
Output : 5:30 —– Half past Five

Example 6:
Enter Hours : 8
Enter Minutes : 45
Output : 8:45 —– Quarter to Nine

Example 7:
Enter Hours : 2
Enter Minutes : 60
Invalid Input !

Example 8:
Enter Hours : 4
Enter Minutes : 0
Output : 4:0 —– Four ‘O’ clock

## [Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks