#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
// Initialize distance and speed
double distance = 30; // (in km)
double speed = 90; // (in km/h)
// Check to avoid division by zero
if (speed <= 0) {
std::cerr << "Speed must be greater than zero." << std::endl;
return 1; // exit with error code
}
// Calculate time
double tm = distance / speed;
std::cout << "Time required: " << std::fixed << std::setprecision(2)
<< tm << " hours" << std::endl;
// Optional: convert to hours and minutes
int hours = static_cast<int>(std::floor(tm));
int minutes = static_cast<int>(std::round((tm - hours) * 60));
std::cout << "Which is approximately "
<< hours << " hours and " << minutes << " minutes."
<< std::endl;
}
/*
run:
Time required: 0.33 hours
Which is approximately 0 hours and 20 minutes.
*/