gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Write a program in C++ that inputs distance travelled in miles and speed of vehicle in…
- User
Write a program in C++ that inputs distance travelled in miles and speed of vehicle in mph.It then calculates time required to reach the destination and prints it
- Assistant
Here is an example program in C++ that takes the distance travelled in miles and the speed of the vehicle in mph, and calculates the time required to reach the destination:
cpp#include <iostream> int main() { double distance, speed; // Input distance and speed std::cout << "Enter the distance travelled (in miles): "; std::cin >> distance; std::cout << "Enter the speed of the vehicle (in mph): "; std::cin >> speed; // Calculate time required to reach the destination double time = distance / speed; // Print the time std::cout << "Time required to reach the destination: " << time << " hours" << std::endl; return 0; }In this program, we use the variables
distanceandspeedto store the user input for distance travelled and speed of the vehicle, respectively. We then calculate the time required by dividing the distance by the speed (as time = distance / speed). Finally, the program outputs the calculated time in hours using the statement.