Home Java Java-conversion Convert Miles To Kilometers



Convert Miles To Kilometers
Posted on: February 27, 2008 at 12:00 AM
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. 

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.

Related Tags for Convert Miles To Kilometers:
cconvertiohelpthisprogramtolearnrameareilsectioninconvertingmpsesmeproskisllmeterfolloweaarrtwingssthprmionomogrolo


More Tutorials from this section

Ask Questions?    Discuss: Convert Miles To Kilometers  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.