Calculate the average speed of a car trip given the distance traveled and the time taken. Scan the distance in kilometers and the time in hours from the user input. Print the average speed rounded to 2 decimal places in kilometers per hour.
Arithmetic operations
Calculating average speed from distance and time
Report a typo
Sample Input 1:
100
2Sample Output 1:
50.00Sample Input 2:
250
3.5Sample Output 2:
71.43Write a program in Java 25
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the distance traveled in kilometers
double distance = scanner.nextDouble();
// Read the time taken in hours
double time = scanner.nextDouble();
// TODO: Calculate the average speed in kilometers per hour
double averageSpeed = 0.0;
// Print the average speed rounded to 2 decimal places
System.out.printf("%.2f", averageSpeed);
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.