In the previous article we have discussed about Java Program to Convert Inch to Millimeter and Millimeter to Inch
In this article we will see how to convert Inch to Mile and Mile to Inch by using Java programming language.
Java Program to Convert Inch to Mile and Mile to Inch
Before jumping into the program let’s know the relationship between Inch and Mile and how we can convert Inch to Mile and vice versa.
Generally Inch and Mile are used as unit in case of length measurement.
1 Mile = 63360 Inch 1 Inch = 1.5783e-5 Mile
Formula to convert Inch to Mile.
Mile = Inch / 63360
Formula to convert Mile to Inch.
Inch = Mile * 63360
Let’s see different ways to convert Inch to Mile and Mile to Inch.
Method-1: Java Program to Convert Inch to Mile and Mile to Inch By Using Static Input Value
Approach:
- Declare Inch and Mile value.
- Then convert Inch to Mile and Mile to Inch 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 mile declared double mile = 1; // value of inch declared double inch = 1; //converting inch to mile double mi = inch / 63360; //converting mile to inch double in = mile * 63360; //printing result System.out.println("Value of "+mile+" mile in inch: "+ in); System.out.println("Value of "+inch+" inch in mile: "+ mi); } }
Output: Value of 1.0 mile in inch: 63360.0 Value of 1.0 inch in mile: 1.5782828282828283E-5
Method-2: Java Program to Convert Inch to Mile and Mile to Inch By Using User Input Value
Approach:
- Take user input of Inch and Mile value.
- Then convert Inch to Mile and Mile to Inch 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 mile System.out.println("Enter value of mile: "); double mile = sc.nextDouble(); //Taking the value input of double variable inch System.out.println("Enter value of inch: "); double inch = sc.nextDouble(); //converting inch to mile double mi = inch / 63360; //converting mile to inch double in = mile * 63360; //printing result System.out.println("Value of "+mile+" mile in inch: "+ in); System.out.println("Value of "+inch+" inch in mile: "+ mi); } }
Output: Enter value of mile: 2 Enter value of inch: 22.8 Value of 2.0 mile in inch: 126720.0 Value of 22.8 inch in mile: 3.5984848484848483E-4
Method-3: Java Program to Convert Inch to Mile and Mile to Inch By Using User Defined Method
Approach:
- Take user input of Inch and Mile value.
- Call a user defined method by passing Inch and Mile value as parameter.
- Inside method convert Inch to Mile and Mile to Inch 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 mile System.out.println("Enter value of mile: "); double mile = sc.nextDouble(); //Taking the value input of double variable inch System.out.println("Enter value of inch: "); double inch = sc.nextDouble(); //calling user defined method convert() convert(mile, inch); } //convert() method to convert mile to inch and vice versa public static void convert(double mile, double inch) { //converting inch to mile double mi = inch / 63360; //converting mile to inch double in = mile * 63360; //printing result System.out.println("Value of "+mile+" mile in inch: "+ in); System.out.println("Value of "+inch+" inch in mile: "+ mi); } }
Output: Enter value of mile: 3 Enter value of inch: 3000000 Value of 3.0 mile in inch: 190080.0 Value of 3000000.0 inch in mile: 47.34848484848485
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: