Integer division and remainder split a number into parts.
What to build
A program that turns a number of seconds into days, hours, minutes and seconds.
Requirements
- Read one number of seconds as an
int64. - Print
D days, HH:MM:SS, where HH, MM and SS have at least two digits. - Always use the word
days, even for 0 or 1 day.
Input
One whole number of seconds.
Output
One line.
Constraints
0 ≤ seconds ≤ 10000000000.
Example
Input:
3661
Output:
0 days, 01:01:01
Why it works
s / 86400 gives whole days. s % 86400 is what is left, and the same idea splits it into hours, minutes and seconds. int64 is needed because the input can be larger than an int holds here.
Examples
Example 1 · Input
3661
Expected output
0 days, 01:01:01
Example 2 · Input
90061
Expected output
1 days, 01:01:01
Hints
Hint 1
Use / and % on an int64: days = s / 86400, then take the remainder.
Hint 2
Print with %02d to pad hours, minutes and seconds to two digits.
Solution
Show one correct solution (try it yourself first)
package main
import "fmt"
func main() {
var s int64
fmt.Scan(&s)
days := s / 86400
rest := s % 86400
h := rest / 3600
m := rest % 3600 / 60
sec := rest % 60
fmt.Printf("%d days, %02d:%02d:%02d\n", days, h, m, sec)
}