In the previous article we have discussed about Java Program to Convert Kilometer to Mile and Mile to Kilometer
In this article we will see how to convert Kilometer to Millimeter and Millimeter to Kilometer by using Java programming language.
Java Program to Convert Kilometer to Millimeter and Millimeter to Kilometer
Before jumping into the program let’s know the relationship between Kilometer and Millimeter and how we can convert Kilometer to Millimeter and vice versa.
Generally Kilometer and Millimeter are used as unit in case of distance measurement.
1 Kilometer = 1000000 Millimeter 1 Millimeter = 1e-6 Kilometer
Formula to convert Kilometer to Millimeter.
Millimeter = Kilometer * 1000000
Formula to convert Millimeter to Kilometer.
Kilometer = Millimeter / 1000000
Let’s see different ways to convert Kilometer to Millimeter and Millimeter to Kilometer.
Method-1: Java Program to Convert Kilometer to Millimeter and Millimeter to Kilometer By Using Static Input Value
Approach:
- Declare Kilometer and Millimeter value.
- Then convert Kilometer to Millimeter and Millimeter 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 millimeter declared double millimeter = 1; //value of kilometer declared double kilometer = 1; //converting kilometer to millimeter double mm = kilometer*1000000; //converting millimeter to kilometer double km = millimeter/1000000; //printing result System.out.println("Value of "+millimeter+" millimeter in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in millimeter: "+ mm); } }
Output: Value of 1.0 millimeter in kilometer: 1.0E-6 Value of 1.0 kilometer in millimeter: 1000000.0
Method-2: Java Program to Convert Kilometer to Millimeter and Millimeter to Kilometer By Using User Input Value
Approach:
- Take user input of Kilometer and Millimeter value.
- Then convert Kilometer to Millimeter and Millimeter 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 millimeter System.out.println("Enter value of millimeter: "); double millimeter = sc.nextDouble(); //Taking the value input of double variable kilometer System.out.println("Enter value of kilometer: "); double kilometer = sc.nextDouble(); //converting kilometer to millimeter double mm = kilometer*1000000; //converting millimeter to kilometer double km = millimeter/1000000; //printing result System.out.println("Value of "+millimeter+" millimeter in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in millimeter: "+ mm); } }
Output: Enter value of millimeter: 88880000 Enter value of kilometer: 567 Value of 8.888E7 millimeter in kilometer: 88.88 Value of 567.0 kilometer in millimeter: 5.67E8
Method-3: Java Program to Convert Kilometer to Millimeter and Millimeter to Kilometer By Using User Defined Method
Approach:
- Take user input of Kilometer and Millimeter value.
- Call a user defined method by passing Kilometer and Millimeter value as parameter.
- Inside method convert Kilometer to Millimeter and Millimeter 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 millimeter System.out.println("Enter value of millimeter: "); double millimeter = 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(millimeter, kilometer); } //convert() method to convert millimeter to kilometer and vice versa public static void convert(double millimeter, double kilometer) { //converting kilometer to millimeter double mm = kilometer*1000000; //converting millimeter to kilometer double km = millimeter/1000000; //printing result System.out.println("Value of "+millimeter+" millimeter in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in millimeter: "+ mm); } }
Output: Enter value of millimeter: 9000500 Enter value of kilometer: 4.5 Value of 9000500.0 millimeter in kilometer: 9.0005 Value of 4.5 kilometer in millimeter: 4500000.0
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: