Java Program to Convert Mile to Yard and Yard to Mile

In the previous article we have discussed about Java Program to Convert Kilometer to Nanometer and Nanometer to Kilometer

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

Java Program to Convert Mile to Yard and Yard to Mile

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

Generally Mile is used to measure distance and Yard is used for length measurement.

1 Yard =  0.000568182 Mile
1 Mile =  1760 Yard

Formula to convert Mile to Yard.

Yard = Mile * 1760

Formula to convert Yard to Mile.

Mile = Yard / 1760

Let’s see different ways to convert Mile to Yard and Yard to Mile.

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

Approach:

  • Declare Mile and Yard value.
  • Then convert Yard to Mile and Mile to Yard 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 yard declared
        double yard = 1;
        //value of mile declared 
        double mile = 1;

        //converting yard to mile
        double mi = yard / 1760;
        //converting mile to yard
        double y = mile * 1760;
        //printing result
        System.out.println("Value of "+mile+" mile in yard: "+ y);   
        System.out.println("Value of "+yard+" yard in mile: "+ mi);   
   }
}
Output:

Value of 1.0 mile in yard: 1760.0
Value of 1.0 yard in mile: 5.681818181818182E-4

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

Approach:

  • Take user input of Mile and Yard value.
  • Then convert Yard to Mile and Mile to Yard 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 yard
        System.out.println("Enter value of yard: ");  
        double yard = sc.nextDouble();

        //converting yard to mile
        double mi = yard / 1760;
        //converting mile to yard
        double y = mile * 1760;
        //printing result
        System.out.println("Value of "+mile+" mile in yard: "+ y);   
        System.out.println("Value of "+yard+" yard in mile: "+ mi);   
   }
}
Output:

Enter value of mile: 
20
Enter value of yard: 
2000
Value of 20.0 mile in yard: 35200.0
Value of 2000.0 yard in mile: 1.1363636363636365

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

Approach:

  • Take user input of Mile and Yard value.
  • Call a user defined method by passing Mile and Yard value as parameter.
  • Inside method convert Yard to Mile and Mile to Yard 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 yard
        System.out.println("Enter value of yard: ");  
        double yard = sc.nextDouble();
        //calling user defined method convert()
        convert(yard, mile);
   }
   
   //convert() method to convert yard to mile and vice versa
   public static void convert(double yard, double mile)
   {
        //converting yard to mile
        double mi = yard / 1760;
        //converting mile to yard
        double y = mile * 1760;
        //printing result
        System.out.println("Value of "+mile+" mile in yard: "+ y);   
        System.out.println("Value of "+yard+" yard in mile: "+ mi);   
   }
}
Output:

Enter value of mile: 
1000
Enter value of yard: 
10000
Value of 1000.0 mile in yard: 1760000.0
Value of 10000.0 yard in mile: 5.681818181818182

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: