Calculating average speed from distance and time

Report a typo

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.

Sample Input 1:

100
2

Sample Output 1:

50.00

Sample Input 2:

250
3.5

Sample Output 2:

71.43
Write 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