Calculate speed in Java
Input - Question 20
In this question, we will see how to input distance in kilometer, time in minutes in Java programming using the java input and find the speed. To know more about java input click on the java input lesson.
Q20) Write a program in Java to input distance in kilometer, time in minutes and find the speed.
Formula: Speed = Distance / Time
Program
import java.util.Scanner;
public class Q20
{
public static void main(String args[])
{
int d,t;
float s;
Scanner sc=new Scanner(System.in);
System.out.print("Enter distance in km ");
d=sc.nextInt();
System.out.print("Enter time in minutes ");
t=sc.nextInt();
s=d/(float)t;
System.out.print("Speed = " + s + " km per minute");
}
}
Output
Enter distance in km 70 Enter time in minutes 50 Speed = 1.4 km per minute