Java Program to Convert Nanometer to Mile and Mile to Nanometer

In the previous article we have discussed about Java Program to Convert Nanometer to Micrometer and Micrometer to Nanometer

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

Java Program to Convert Nanometer to Mile and Mile to Nanometer

Before jumping into the program let’s know the relationship between Nanometer and Mile and how we can convert Nanometer to Mile and vice versa.

Generally Nanometer is used to measure very small things like atoms and molecules and Mile is used to measure distance.

1 Nanometer = 6.21371e-13 Mile
1 Mile =  1.609e+12 Nanometer

Formula to convert Nanometer to Mile.

Mile = Nanometer / 1.609e+12

Formula to convert Mile to Nanometer.

Nanometer = Mile * 1.609e+12

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

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

Approach:

  • Declare Nanometer and Mile value.
  • Then convert Nanometer to Mile and Mile to Nanometer 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);
        //value of mile declared
        double mile = 1;
        //value of nanometer declared 
        double nanometer = 1;

        //converting nanometer to mile
        double mi = nanometer / 1.609e+12;
        //converting mile to nanometer
        double nm = mile * 1.609e+12;
        //printing result
        System.out.println("Value of "+mile+" mile in nanometer: "+ nm);   
        System.out.println("Value of "+nanometer+" nanometer in mile: "+ mi);   
   }
}
Output:

Value of 1.0 mile in nanometer: 1.609E12
Value of 1.0 nanometer in mile: 6.215040397762585E-13

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

Approach:

  • Take user input of Nanometer and Mile value.
  • Then convert Nanometer to Mile and Mile to Nanometer 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 mile
        System.out.println("Enter value of mile: ");  
        double mile = sc.nextDouble();
        //Taking the value input of double variable nanometer
        System.out.println("Enter value of nanometer: ");  
        double nanometer = sc.nextDouble();

        //converting nanometer to mile
        double mi = nanometer / 1.609e+12;
        //converting mile to nanometer
        double nm = mile * 1.609e+12;
        //printing result
        System.out.println("Value of "+mile+" mile in nanometer: "+ nm);   
        System.out.println("Value of "+nanometer+" nanometer in mile: "+ mi);   
   }
}
Output:

Enter value of mile: 
2
Enter value of nanometer: 
200000
Value of 2.0 mile in nanometer: 3.218E12
Value of 200000.0 nanometer in mile: 1.2430080795525171E-7

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

Approach:

  • Take user input of Nanometer and Mile value.
  • Call a user defined method by passing Nanometer and Mile value as parameter.
  • Inside method convert Nanometer to Mile and Mile to Nanometer 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 mile
        System.out.println("Enter value of mile: ");  
        double mile = sc.nextDouble();
        //Taking the value input of double variable nanometer
        System.out.println("Enter value of nanometer: ");  
        double nanometer = sc.nextDouble();
         //calling user defined method convert()
        convert(mile, nanometer);
   }
   
   //convert() method to convert mile to nanometer and vice versa
   public static void convert(double mile, double nanometer)
   {
        //converting nanometer to mile
        double mi = nanometer / 1.609e+12;
        //converting mile to nanometer
        double nm = mile * 1.609e+12;
        //printing result
        System.out.println("Value of "+mile+" mile in nanometer: "+ nm);   
        System.out.println("Value of "+nanometer+" nanometer in mile: "+ mi);   
   }
}
Output:

Enter value of mile: 
4
Enter value of nanometer: 
800000
Value of 4.0 mile in nanometer: 6.436E12
Value of 800000.0 nanometer in mile: 4.972032318210069E-7

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: