Java Program to Convert Centimeter to Nautical Mile and Nautical Mile to Centimeter

In the previous article, we have discussed about Java Program to Convert Centimeter to Millimeter and Millimeter to Centimeter

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

Java Program to Convert Centimeter to Nautical Mile and Nautical Mile to Centimeter

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

Generally Centimeter and Nautical Mile are used as unit in case of distance measurement.

1 Centimeter =  6.2137e-6 Nautical Mile
1 Nautical Mile =  160934 Centimeter

Formula to convert Centimeter to Nautical Mile.

Nautical Mile = Centimeter * 5.3996 * .0000001

Formula to convert Nautical Mile to Centimeter.

Centimeter  = Nautical Mile * 185200

Let’s see different ways to convert Centimeter to Nautical Mile and Nautical Mile to Centimeter.

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

Approach:

• Declare Centimeter and Nautical Mile value.
• Then convert Centimeter to Nautical Mile and Nautical Mile to Centimeter 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);
//centimeter value declared
double centimeter = 10000;
//nautical Mile value declared
double nauticalMile = 1.5;

//converting centimeter to Nautical Mile
double nm = centimeter * 5.3996 * .0000001;
//converting Nautical Mile to centimeter
double cm = nauticalMile * 185200;
//printing result
System.out.println("Value of "+centimeter+" centimeter in nautical mile: "+ nm);
System.out.println("Value of "+nauticalMile+" nautical mile in centimeter: "+ cm);
}
}
Output:
Value of 10000.0 centimeter in nautical mile: 0.0053996
Value of 1.5 nautical mile in centimeter: 277800.0

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

Approach:

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

//converting centimeter to Nautical Mile
double nm = centimeter * 5.3996 * .0000001;
//converting Nautical Mile to centimeter
double cm = nauticalMile * 185200;
//printing result
System.out.println("Value of "+centimeter+" centimeter in nautical mile: "+ nm);
System.out.println("Value of "+nauticalMile+" nautical mile in centimeter: "+ cm);
}
}
Output:

Enter value of centimeter:
3.5
Enter value of nautical mile:
4.5
Value of 3.5 centimeter in nautical mile: 1.88986E-6
Value of 4.5 nautical mile in centimeter: 833400.0


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

Approach:

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

//convert() method to convert centimeter to Nautical Mile and vice versa
public static void convert(double centimeter, double nauticalMile)
{
//converting centimeter to Nautical Mile
double nm = centimeter * 5.3996 * .0000001;
//converting Nautical Mile to centimeter
double cm = nauticalMile * 185200;
//printing result
System.out.println("Value of "+centimeter+" centimeter in nautical mile: "+ nm);
System.out.println("Value of "+nauticalMile+" nautical mile in centimeter: "+ cm);
}
}
Output:
Enter value of centimeter:
100000
Enter value of nautical mile:
2
Value of 100000.0 centimeter in nautical mile: 0.053995999999999995
Value of 2.0 nautical mile in centimeter: 370400.0

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: