BITFRENZY

a blog dedicated to technology and programming to shape nation's future.

Breaking

Post Top Ad

Sunday, August 4, 2019

The Time in Words Hackerrank Solution in c

Given the time in numerals we may convert it into words, as shown below:
At , use o' clock. For , use past, and for  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. It should return a time string as described.
timeInWords has the following parameter(s):
  • h: an integer representing hour of the day
  • m: an integer representing minutes after the hour
Input Format
The first line contains , the hours portion The second line contains , the minutes portion
Constraints
Output Format
Print the time in words as described.
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<math.h>
int main()
{
int h,m;
scanf("%d\n %d\n",&h,&m);
char *count[]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fiveteen","sixteen","seventeen","eighteen","nineteen","twenty","twenty one","twenty two","twenty three","twenty four","twenty five","twenty six","twenty seven","twenty eight","twenty nine"};
if(m==0)
printf("%s o' clock",count[h-1]);
else if(m>30)
{
if(m==45)
printf("quarter to %s",count[h]);
if(m==59)
printf("%s minutes to %s",count[m-1],count[h]);
else if(m!=45 && m!=59)
printf("%s minutes to %s",count[60-m-1],count[h]);
}
else if(m<=30)
{
if(m==1)
printf("%s minute past %s",count[m-1],count[h-1]);
if(m==15)
printf("quarter past %s",count[h-1]);
if(m==30)
printf("half past %s",count[h-1]);
else if(m!=15 && m!=30 && m!=1)
printf("%s minutes past %s",count[m-1],count[h-1]);
}
return 0;
}

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages