Convert Miles To Kilometers

In this section, you will learn to convert Miles to Kilometers. The following program helps you in converting Miles to Kilometers.

Convert Miles To Kilometers

In this section, you will learn to convert Miles to Kilometers. The following program helps you in converting Miles to Kilometers.

Convert Miles To Kilometers

Convert Miles To Kilometers

     

In this section, you will learn to convert Miles to Kilometers. The following program helps you in converting Miles to Kilometers. 

Code Description:

To convert Miles to Kilometers we have used a formula "km = miles * 1.609344;". The compiler will ask to enter the number of Miles from the keyboard due to the method  "
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));". Here we have taken miles to be of type float. To avoid loss of precession we have cast the output to be in float.

Here is the code of the program:

import java.io.*;
import java.util.*;

class MilesToKms{
  public static void main (String[] args)throws Exception{
  //1 mile = 1.609 344 kilometer;
  BufferedReader bf = new BufferedReader(new 
  InputStreamReader
(System.in));
  System.out.print("Enter a number of miles: ");
  float miles = Float.parseFloat(bf.readLine());
  float km = miles * 1.609344f;
  System.out.println(miles + " miles is " + km + " kilometers.");
  }
}

Output of the program:

C:\unique>javac MilesToKms.java

C:\unique>java MilesToKms
Enter a number of miles: 1
1.0 miles is 1.609344 kilometers.

C:\unique>

Download this example.