Find the time on a 24-hour clock 1000 minutes after 22:30, and 45 minutes before 00:10.
Solution
main.c
#include <stdio.h>
void add_minutes(int h, int m, int delta) {
int total = h * 60 + m + delta;
total = ((total % 1440) + 1440) % 1440; /* wrap around midnight, also for negative delta */
printf("%02d:%02d %+d min -> %02d:%02d\n", h, m, delta, total / 60, total % 60);
}
int main(void) {
add_minutes(22, 30, 1000);
add_minutes(0, 10, -45);
return 0;
}
Output Verified by compiling and running
22:30 +1000 min -> 15:10
00:10 -45 min -> 23:25
A day has 1440 minutes, so the clock is arithmetic modulo 1440. Adding 1440 before the second % fixes C's negative remainders: −35 becomes 1405 minutes = 23:25.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!