In the previous article we have discussed about Java Program to Convert Kilometer to Millimeter and Millimeter to Kilometer
In this article we will see how to convert Kilometer to Nautical Mile and Nautical Mile to Kilometer by using Java programming language.
Java Program to Convert Kilometer to Nautical Mile and Nautical Mile meter to Kilometer
Before jumping into the program let’s know the relationship between Kilometer and Nautical Mile and how we can convert Kilometer to Nautical Mile and vice versa.
Generally Kilometer and Nautical Mile are used as unit in case of distance measurement.
1 Kilometer = 0.539957 Nautical Mile 1 Nautical Mile = 1.852 Kilometer
Formula to convert Kilometer to Nautical Mile.
Nautical Mile = Kilometer / 1.852 (OR) Kilometer * 0.539957
Formula to convert Mile to Kilometer.
Kilometer = Nautical Mile * 1.852
Let’s see different ways to convert Kilometer to Nautical Mile and Nautical Mile to Kilometer.
Method-1: Java Program to Convert Kilometer to Nautical Mile and Nautical Mile meter to Kilometer By Using Static Input Value
Approach:
- Declare Kilometer and Nautical Mile value.
- Then convert Kilometer to Nautical Mile and Nautical Mile 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 nautical mile declared double nauticalmile = 1; //value of kilometer declared double kilometer = 1; //converting kilometer to nautical mile double nmi = kilometer / 1.852; //converting nautical mile to kilometer double km = nauticalmile * 1.852; //printing result System.out.println("Value of "+nauticalmile+" nautical mile in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in nautical mile: "+ nmi); } }
Output: Value of 1.0 nautical mile in kilometer: 1.852 Value of 1.0 kilometer in nautical mile: 0.5399
Method-2: Java Program to Convert Kilometer to Nautical Mile and Nautical Mile meter to Kilometer By Using User Input Value
Approach:
- Take user input of Kilometer and Nautical Mile value.
- Then convert Kilometer to Nautical Mile and Nautical Mile 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 nauticalmile System.out.println("Enter value of nautical mile: "); double nauticalmile = sc.nextDouble(); //Taking the value input of double variable kilometer System.out.println("Enter value of kilometer: "); double kilometer = sc.nextDouble(); //converting kilometer to nautical mile double nmi = kilometer / 1.852; //converting nautical mile to kilometer double km = nauticalmile * 1.852; //printing result System.out.println("Value of "+nauticalmile+" nautical mile in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in nautical mile: "+ nmi); } }
Output: Enter value of nautical mile: 5 Enter value of kilometer: 8 Value of 5.0 nautical mile in kilometer: 9.26 Value of 8.0 kilometer in nautical mile: 4.3196
Method-3: Java Program to Convert Kilometer to Nautical Mile and Nautical Mile meter to Kilometer By Using User Defined Method
Approach:
- Take user input of Kilometer and Nautical Mile value.
- Call a user defined method by passing Kilometer and Nautical Mile value as parameter.
- Inside method convert Kilometer to Nautical Mile and Nautical Mile 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 nauticalmile System.out.println("Enter value of nautical mile: "); double nauticalmile = 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(nauticalmile, kilometer); } //convert() method to convert nautical mile to kilometer and vice versa public static void convert(double nauticalmile, double kilometer) { //converting kilometer to nautical mile double nmi = kilometer / 1.852; //converting nautical mile to kilometer double km = nauticalmile * 1.852; //printing result System.out.println("Value of "+nauticalmile+" nautical mile in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in nautical mile: "+ nmi); } }
Output: Enter value of nautical mile: 4.4 Enter value of kilometer: 2.2 Value of 4.4 nautical mile in kilometer: 8.1488 Value of 2.2 kilometer in nautical mile: 1.1879
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: