As there will be 2 angles the small and large between both hands, such that large small = 360. You have to return the smaller one as a result. time given is in 24 hrs format we have to convert into 12 hrs. For example If time is 00:30 then the time in 12 hrs format will be 12:30 and the angle between hour hand and minute hand is 165 degrees. but I am unable to code in 24 hrs format to 12hrs format its only working for 12 hrs format. if I am giving input as 00:30 the angle is coming as 525. and for if I put 12:20 its working fine.. Can someone please help me what changes I had to made.
#include <stdio.h>
float time_angle(int XX, int YY){
float result = 0.5*((60 * XX) YY)-(6*YY);
if (XX > 23 || YY > 59)
{
return 1;
}
if (XX >= 12)
{
if (XX > 12)
{
XX -= 12;
}
}
return result;
}
int main()
{
int XX, YY;
scanf("%d", &XX);
scanf("%d", &YY);
// printf("%d:%d",XX,YY);
int result = 360-time_angle(XX, YY);
printf("%d",result);
return 0;
}
CodePudding user response:
I think this does the job:
#include <stdio.h>
#include <stdlib.h>
static void get_time(int *hh, int *mm)
{
if (scanf("-%*[ :.]-", hh, mm) != 2)
{
fprintf(stderr, "failed to read time\n");
exit(EXIT_FAILURE);
}
if (*hh < 0 || *hh > 24 || *mm < 0 || *mm > 59 || (*hh == 24 && *mm != 0))
{
fprintf(stderr, "invalid time value %.2d:%.2d\n", *hh, *mm);
exit(EXIT_FAILURE);
}
}
/*
** The hour hand of a clock moves 30 degrees per hour.
** It moves 0.5 degree per minute.
** Alternatively, the hour hand moves 0.5 (60 * hours minutes).
** The minute hand of a clock moves 6 degrees per minute.
*/
int main(void)
{
int hh;
int mm;
get_time(&hh, &mm);
int rh = hh % 12;
double hh_angle = 30.0 * rh 0.5 * mm;
double mm_angle = 6.0 * mm;
double result;
if (hh_angle > mm_angle)
result = hh_angle - mm_angle;
else
result = mm_angle - hh_angle;
if (result > 180.0)
result = 360.0 - result;
printf("Angle between hands of clock at %.2d:%.2d = %.2f\n", hh, mm, result);
return 0;
}
That code was in clk17.c
, compiled to program clk17
. Sample outputs, generated from a file data
containing:
1:5
1:6
1:7
1:8
2:10
2:11
2:12
3:16
3:17
4:21
4:22
4:23
5:27
5:28
6:32
6:33
7:37
7:38
7:39
8:43
8:44
9:48
9:49
9:50
10:54
10:55
11:58
11:59
12:00
14:37
24:00
24:01
13:61
The shell script (command) and the output:
$ while read data; do echo $data | clk17; done < data
Angle between hands of clock at 01:05 = 2.50
Angle between hands of clock at 01:06 = 3.00
Angle between hands of clock at 01:07 = 8.50
Angle between hands of clock at 01:08 = 14.00
Angle between hands of clock at 02:10 = 5.00
Angle between hands of clock at 02:11 = 0.50
Angle between hands of clock at 02:12 = 6.00
Angle between hands of clock at 03:16 = 2.00
Angle between hands of clock at 03:17 = 3.50
Angle between hands of clock at 04:21 = 4.50
Angle between hands of clock at 04:22 = 1.00
Angle between hands of clock at 04:23 = 6.50
Angle between hands of clock at 05:27 = 1.50
Angle between hands of clock at 05:28 = 4.00
Angle between hands of clock at 06:32 = 4.00
Angle between hands of clock at 06:33 = 1.50
Angle between hands of clock at 07:37 = 6.50
Angle between hands of clock at 07:38 = 1.00
Angle between hands of clock at 07:39 = 4.50
Angle between hands of clock at 08:43 = 3.50
Angle between hands of clock at 08:44 = 2.00
Angle between hands of clock at 09:48 = 6.00
Angle between hands of clock at 09:49 = 0.50
Angle between hands of clock at 09:50 = 5.00
Angle between hands of clock at 10:54 = 3.00
Angle between hands of clock at 10:55 = 2.50
Angle between hands of clock at 11:58 = 11.00
Angle between hands of clock at 11:59 = 5.50
Angle between hands of clock at 12:00 = 0.00
Angle between hands of clock at 14:37 = 143.50
Angle between hands of clock at 24:00 = 0.00
failed to read time
invalid time value 24:01
invalid time value 13:61
$