Click here to Skip to main content
15,888,156 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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.The time given to you is in 24 hrs format, you have to convert it into 12hrs format then find the angle.
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.

I have written the code but not able to find how to add 24 hrs to 12hrs convert.


What I have tried:

#include <stdio.h>

float time_angle(int XX, int YY){  
    float result = 0.5*((60 * XX) + YY)-(6*YY);
    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;  
}
Posted
Updated 10-Jul-22 19:26pm
Comments
Richard MacCutchan 5-Jun-22 3:27am    
If the hour is greater than 12 then you should subtract 12 to get the hour hand value. If it is equal to zero then set it to 12.
pppp333000 5-Jun-22 3:30am    
How to make changes in this code can you please show?
Richard MacCutchan 5-Jun-22 3:34am    
It would probably help if you used sensible names for your variables:
int hour, minute;

// Then ...
if (hour > 12)
{
    hour -= 12;
}
if (hour == 0)
{
    hour = 12;
}

1 solution

You seem to know how to code this math, so I will leave that to you.

You seem to be having difficulty with the logic of the adjustment from 12 to 24 hour.

Here is some logic that I might use.

----------

Example:

For the minute hand:
360 degrees / 60 minutes = 6 degrees per minute
00:30 = 30 minutes
30 minutes X 6 degrees per minute = 180 degrees
The minute hand will have moved 180 degrees.

For the hour hand:
360 degrees / 12 hours = 30 degrees per hour
30 degrees per hour / 60 minutes per hour = 0.5 degrees per minute
00:30 = 30 minutes
30 minutes X 0.5 degrees per minute = 15 degrees
The hour hand will have moved 15 degrees.

The andle difference between the two:
Minute hand = 180 degrees
Hour hand = 15 degrees
180 - 15 = 165 degrees.

I see nothing wrong with your math resulting in a difference of 165 degrees.

The minute hand simply continues to go around.

----------

Example:

If the time is 1:30 O'Clock (am in the morning) then the hour hand would be15 degrees past the one hour mark.

The one hour mark is at 30 degrees.
Thus, the hour hand would be at 30 + 15 = 45 degrees.

The minute hand is at 180 degrees.

The difference between the two would be 180 - 45 = 135 degrees.

----------

Example:

If the time is 1:30 O'Clock (pm in the evening) then the hour hand would be15 degrees past the one hour mark.

The one hour mark is at 30 degrees.
Thus, the hour hand would be at 30 + 15 = 45 degrees.

The minute hand is at 180 degrees.

The difference between the two would be 180 - 45 = 135 degrees.

----------

Result:

Morning 1:30 would have the same angle as Evening 1:30.

No need to change code for that calculation.

----------

Adjusting for 12 to 24 hour:

But, if you are converting to 24 hour
while still using a 12 hour clock face,
then if you want the user to know that it is am or pm (morning or evening),
subtract 12 from the time
and if the result is less than zero or equal to zero, it is am (or morning),
else it is pm (or evening).

----------

Some military clocks use a 24 hour clock face.

Divide your hour hand position by 2 to get it's angle.

Leave the minute hand at the angle where it is.

Find the difference between the two.

---------

I hope that answers your question.
 
Share this answer
 
v2
Comments
CHill60 11-Jul-22 4:50am    
I'm not sure that your logic around if the result is less than zero it is morning otherwise it is pm - it's a lot easier to say if the number is less than 12 it's am otherwise it is pm and in the latter case subtract 12. You've also missed the special case of midnight (00:00hrs)
Member 15078716 11-Jul-22 13:06pm    
Thank you.

Changed to

and if the result is less than zero or equal to zero, it is am (or morning),
else it is pm (or evening).

Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900