Split a number of seconds into a time.
What to build
A program that uses division and remainders together.
Requirements
- Read a number of seconds.
- Print the whole hours, minutes and seconds, separated by colons.
Input
One whole number.
Output
H:M:S, with no leading zeros.
Constraints
The number is from 0 to 1000000.
Example
Input:
3661
Output:
1:1:1
Why it works
Integer division gives the whole hours, and the remainder is what is left to share into minutes and seconds.
Examples
Example 1 · Input
3661
Expected output
1:1:1
Example 2 · Input
59
Expected output
0:0:59
Hints
Hint 1
There are 3600 seconds in an hour.
Hint 2
% 3600 gives what is left after the whole hours.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int total;
scanf("%d", &total);
int h = total / 3600;
int m = (total % 3600) / 60;
int s = total % 60;
printf("%d:%d:%d\n", h, m, s);
return 0;
}