package main
import (
"fmt"
"math"
"os"
)
func main() {
// Initialize distance and speed
distance := 30.0 // (in km)
speed := 90.0 // (in km/h)
// Check to avoid division by zero
if speed <= 0 {
fmt.Fprintln(os.Stderr, "Speed must be greater than zero.")
os.Exit(1) // exit with error code
}
// Calculate time
tm := distance / speed
fmt.Printf("Time required: %.2f hours\n", tm)
// Optional: convert to hours and minutes
hours := int(math.Floor(tm))
minutes := int(math.Round((tm - float64(hours)) * 60))
fmt.Printf("Which is approximately %d hours and %d minutes.\n", hours, minutes)
}
/*
run:
Time required: 0.33 hours
Which is approximately 0 hours and 20 minutes.
*/