Write a program to accept the distance travelled by a person in metres. Display the output in Km and Mt (Hint - 1 Km= 1000 mts)
Ex: Input: 5602 mts
Output: 5 Km and 602 mts
(java programming language only)
Answers
Program:
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
int m;
System.out.print("Enter distance in metres : ");
m = Sc.nextInt();
int km = m / 1000;
int mt = m % 1000;
System.out.print("Distance : " + km + " Km " + mt + " mts");
}
}
Output:
Enter distance in metres : 5602
Distance : 5 Km 602 mts
import java.util.Scanner;
class distanceCalculator
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter distance: ");
double distance = sc.nextDouble();
System.out.println((int)(Math.floor(distance/1000)) + " Km and " + (int)(distance%1000) + " mts");
}
}