Mile to cm – Java Program to Convert Centimeter to Mile and Mile to Centimeter

Mile to cm: In the previous article, we have discussed about Java Program to Convert Fahrenheit to kelvin and Kelvin to Fahrenheit

In this article we will see how to convert Centimeter to Mile and Mile to Centimeter by using Java programming language.

Java Program to Convert Centimeter to Mile and Mile to Centimeter

How to convert cm to miles: Before jumping into the program let’s know the relationship between Centimeter and Mile and how we can convert Centimeter to Mile and vice versa.

Generally Centimeter and Mile are used as unit in case of distance measurement.

1 Centimeter =  6.2137e-6 Mile
1 Mile =  160934 Centimeter

Formula to convert Centimeter to Mile.

Mile = Centimeter * 6.21371 * .0000001

Formula to convert Mile to Centimeter.

Centimeter  = Mile * 160934

Let’s see different ways to convert Centimeter to Mile and Mile to Centimeter.

Method-1: Java Program to Convert Centimeter to Mile and Mile to Centimeter By Using Static Input Value

Approach:

  • Declare Centimeter and Mile value.
  • Then convert Centimeter to Mile and Mile to Centimeter by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of centimeter declared
        double centimeter = 1;
        //value of mile declared  
        double mile = 1;
        
        //converting centimeter to mile
        double mi = centimeter * 6.21371 * .0000001; 
        //converting mile to centimeter
        double cm = mile * 160934;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in mile: "+ mi);   
        System.out.println("Value of "+mile+" mile in centimeter: "+ cm);   
   }
}
Output:
Value of 1.0 centimeter in mile: 6.213709999999999E-7
Value of 1.0 mile in centimeter: 160934.0

Method-2: Java Program to Convert Centimeter to Mile and Mile to Centimeter By Using User Input Value

Approach:

  • Take user input of Centimeter and Mile value.
  • Then convert Centimeter to Mile and Mile to Centimeter by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable centimeter
        System.out.println("Enter value of centimeter: ");  
        double centimeter = sc.nextDouble();
        //Taking the value input of double variable mile
        System.out.println("Enter value of mile: ");  
        double mile = sc.nextDouble();
        
        //converting centimeter to mile
        double mi = centimeter * 6.21371 * .0000001; 
        //converting mile to centimeter
        double cm = mile * 160934;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in mile: "+ mi);   
        System.out.println("Value of "+mile+" mile in centimeter: "+ cm);   
   }
}
Output:

Enter value of centimeter: 
100000
Enter value of mile: 
1
Value of 100000.0 centimeter in mile: 0.062137099999999994
Value of 1.0 mile in centimeter: 160934.0

Method-3: Java Program to Convert Centimeter to Mile and Mile to Centimeter By Using User Defined Method

Approach:

  • Take user input of Centimeter and Mile value.
  • Call a user defined method by passing Centimeter and Mile value as parameter.
  • Inside method convert Centimeter to Mile and Mile to Centimeter by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable centimeter
        System.out.println("Enter value of centimeter: ");  
        double centimeter = sc.nextDouble();
        //Taking the value input of double variable mile
        System.out.println("Enter value of mile: ");  
        double mile = sc.nextDouble();
         //calling user defined method convert()
        convert(centimeter, mile);
   }
   
   //convert() method to convert centimeter to mile and vice versa
   public static void convert(double centimeter, double mile)
   {
        
        //converting centimeter to mile
        double mi = centimeter * 6.21371 * .0000001; 
        //converting mile to centimeter
        double cm = mile * 160934;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in mile: "+ mi);   
        System.out.println("Value of "+mile+" mile in centimeter: "+ cm);   
   }
}
Output:

Enter value of centimeter: 
99999999
Enter value of mile: 
2.5
Value of 9.9999999E7 centimeter in mile: 62.13709937862899
Value of 2.5 mile in centimeter: 402335.0

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: