Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-conversion Convert Miles To Kilometers

Related Tutorials


 
 

Share on Google+Share on Google+

Convert Miles To Kilometers

Advertisement
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.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 27, 2008

Related Tutorials

Discuss: Convert Miles To Kilometers  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com