In the previous article we have discussed about Java Program to Convert Kilometer to Yard and Yard to Kilometer
In this article we will see how to convert Kilometer to Micrometer and Micrometer to Kilometer by using Java programming language.
Java Program to Convert Kilometer to Micrometer and Micrometer to Kilometer
Before jumping into the program let’s know the relationship between Kilometer and Micrometer and how we can convert Kilometer to Micrometer and vice versa.
Generally Kilometer is used to measure distance and Micrometer is used to measure very small length.
1 Kilometer = 1e+9 Micrometer 1 Micrometer = 1e-9 Kilometer
Formula to convert Kilometer to Micrometer.
Micrometer = Kilometer * 1e+9
Formula to convert Micrometer to Kilometer.
Kilometer = Micrometer * 1e-9
Let’s see different ways to convert Kilometer to Micrometer and Micrometer to Kilometer.
Method-1: Java Program to Convert Kilometer to Micrometer and Micrometer to Kilometer By Using Static Input Value
Approach:
- Declare Kilometer and Micrometer value.
- Then convert Kilometer to Micrometer and Micrometer 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 micrometer declared double micrometer = 1; //value of kilometer declared double kilometer = 1; //converting kilometer to micrometer double um = kilometer * 1e+9; //converting micrometer to kilometer double km = micrometer * 1e-9; //printing result System.out.println("Value of "+micrometer+" micrometer in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in micrometer: "+ um); } }
Output: Value of 1.0 micrometer in kilometer: 1.0E-9 Value of 1.0 kilometer in micrometer: 1.0E9
Method-2: Java Program to Convert Kilometer to Micrometer and Micrometer to Kilometer By Using User Input Value
Approach:
- Take user input of Kilometer and Micrometer value.
- Then convert Kilometer to Micrometer and Micrometer 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 micrometer System.out.println("Enter value of micrometer: "); double micrometer = sc.nextDouble(); //Taking the value input of double variable kilometer System.out.println("Enter value of kilometer: "); double kilometer = sc.nextDouble(); //converting kilometer to micrometer double um = kilometer * 1e+9; //converting micrometer to kilometer double km = micrometer * 1e-9; //printing result System.out.println("Value of "+micrometer+" micrometer in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in micrometer: "+ um); } }
Output: Enter value of micrometer: 5 Enter value of kilometer: 2 Value of 5.0 micrometer in kilometer: 5.0E-9 Value of 2.0 kilometer in micrometer: 2.0E9
Method-3: Java Program to Convert Kilometer to Micrometer and Micrometer to Kilometer By Using User Defined Method
Approach:
- Take user input of Kilometer and Micrometer value.
- Call a user defined method by passing Kilometer and Micrometer value as parameter.
- Inside method convert Kilometer to Micrometer and Micrometer 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 micrometer System.out.println("Enter value of micrometer: "); double micrometer = 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(micrometer, kilometer); } //convert() method to convert micrometer to kilometer and vice versa public static void convert(double micrometer, double kilometer) { //converting kilometer to micrometer double um = kilometer * 1e+9; //converting micrometer to kilometer double km = micrometer * 1e-9; //printing result System.out.println("Value of "+micrometer+" micrometer in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in micrometer: "+ um); } }
Output: Enter value of micrometer: 250000 Enter value of kilometer: 45679 Value of 250000.0 micrometer in kilometer: 2.5E-4 Value of 45679.0 kilometer in micrometer: 4.5679E13
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: