Split a duration given in seconds into days, hours, minutes and seconds.
Solution
main.c
#include <stdio.h>
int main(void) {
long total = 1000000; /* one million seconds */
long days = total / 86400;
long hours = total % 86400 / 3600;
long minutes = total % 3600 / 60;
long seconds = total % 60;
printf("%ld s = %ld days %ld h %ld min %ld s\n", total, days, hours, minutes, seconds);
return 0;
}
Output Verified by compiling and running
1000000 s = 11 days 13 h 46 min 40 s
Integer division gives the whole units and % the remainder, which feeds the next smaller unit. Here integer division is exactly what we want.
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!