# Nanometer to kilometer – Java Program to Convert Kilometer to Nanometer and Nanometer to Kilometer

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

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

## Java Program to Convert Kilometer to Nanometer and Nanometer to Kilometer

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

Generally Kilometer is used to measure distance and Nanometer is used to measure very small length.

1 Kilometer =  1e+12 Nanometer
1 Nanometer =  1e-12 Kilometer

Formula to convert Kilometer to Nanometer.

Nanometer = Kilometer * 1e+12

Formula to convert Nanometer to Kilometer.

Kilometer = Nanometer / 1e+12

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

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

Approach:

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

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

Value of 1.0 nanometer in kilometer: 1.0E-12
Value of 1.0 kilometer in nanometer: 1.0E12


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

Approach:

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

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

Enter value of nanometer:
56789
Enter value of kilometer:
14
Value of 56789.0 nanometer in kilometer: 5.6789E-8
Value of 14.0 kilometer in nanometer: 1.4E13


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

Approach:

• Take user input of Kilometer and Nanometer value.
• Call a user defined method by passing Kilometer and Nanometer value as parameter.
• Inside method convert Kilometer to Nanometer and Nanometer to Kilometer 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 nanometer
System.out.println("Enter value of nanometer: ");
double nanometer = sc.nextDouble();
//Taking the value input of double variable kilometer
System.out.println("Enter value of kilometer: ");
double kilometer = sc.nextDouble();
//calling user defined method convert()
convert(nanometer, kilometer);
}

//convert() method to convert nanometer to kilometer and vice versa
public static void convert(double nanometer, double kilometer)
{
//converting kilometer to nanometer
double nm = kilometer * 1e+12;
//converting nanometer to kilometer
double km = nanometer / 1e+12;
//printing result
System.out.println("Value of "+nanometer+" nanometer in kilometer: "+ km);
System.out.println("Value of "+kilometer+" kilometer in nanometer: "+ nm);
}
}
Output:

Enter value of nanometer:
400000
Enter value of kilometer:
4
Value of 400000.0 nanometer in kilometer: 4.0E-7
Value of 4.0 kilometer in nanometer: 4.0E12


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: