Thursday 19 January 2017

Time to Words...

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.Your program should follow the format of the examples below.

SAMPLE DATA :
 INPUT :                TIME :         3,0
OUTPUT :              3 : 00 Three o’ clock
INPUT :                 TIME :         7,29
OUTPUT :              7 : 29 Twenty nine minutes past seven
INPUT :                 TIME :         6,34
OUTPUT :              6 : 34 Twenty six minutes to seven
INPUT :                 TIME :         12,1
OUTPUT :              12 : 01 One minute past Twelve
 INPUT :                TIME : 12,45
OUTPUT :              12 : 45 Quarter to One
INPUT :                 TIME : 10,59
OUTPUT :              10 : 59 One minute to Eleven
INPUT :                 TIME : 14,60
OUTPUT :              Incorrect Input

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



Solution:-


import java.util.*;
public class Time2words
{  
    public static void main(String args[])
    {  
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter Hours(HH): ");
        int h=sc.nextInt();
        System.out.print("Enter Minutes(MM): ");
        int m=sc.nextInt();
        if((h>=1 && h<=12) && (m>=0 && m<=59)) // checking whether given input is valid or not.
        {
        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 plu, a;
         if(m == 1 || m == 59)
            plu = "Minute";
         else
            plu = "Minutes";
         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
         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
    }
}









No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...