In the previous article we have discussed about Java Program to Convert Inch to Nautical Mile and Nautical Mile to Inch
In this article we will see how to convert Inch to Yard and Yard to Inch by using Java programming language.
Java Program to Convert Inch to Yard and Yard to Inch
Before jumping into the program let’s know the relationship between Inch and Yard and how we can convert Inch to Yard and vice versa.
Generally Inch and Yard are used as unit in case of length measurement.
1 Yard = 36 Inch 1 Inch = 0.0277778 Yard
Formula to convert Inch to Yard.
Yard = Inch / 36 (OR) Inch * 0.0277778
Formula to convert Yard to Inch.
Inch = Yard * 36
Let’s see different ways to convert Inch to Yard and Yard to Inch.
Method-1: Java Program to Convert Inch to Yard and Yard to Inch By Using Static Input Value
Approach:
- Declare Inch and Yard value.
- Then convert Inch to Yard and Yard 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 yard declared double yard = 1; //value of inch declared double inch = 1; //converting inch to yard double y = inch / 36; //converting yard to inch double in = yard * 36; //printing result System.out.println("Value of "+yard+" yard in inch: "+ in); System.out.println("Value of "+inch+" inch in yard: "+ y); } }
Output: Value of 1.0 yard in inch: 36.0 Value of 1.0 inch in yard: 0.027777777777777776
Method-2: Java Program to Convert Inch to Yard and Yard to Inch By Using User Input Value
Approach:
- Take user input of Inch and Yard value.
- Then convert Inch to Yard and Yard 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 yard System.out.println("Enter value of yard: "); double yard = sc.nextDouble(); //Taking the value input of double variable inch System.out.println("Enter value of inch: "); double inch = sc.nextDouble(); //converting inch to yard double y = inch / 36; //converting yard to inch double in = yard * 36; //printing result System.out.println("Value of "+yard+" yard in inch: "+ in); System.out.println("Value of "+inch+" inch in yard: "+ y); } }
Output: Enter value of yard: 3 Enter value of inch: 909090 Value of 3.0 yard in inch: 108.0 Value of 909090.0 inch in yard: 25252.5
Method-3: Java Program to Convert Inch to Yard and Yard to Inch By Using User Defined Method
Approach:
- Take user input of Inch and Yard value.
- Call a user defined method by passing Inch and Yard value as parameter.
- Inside method convert Inch to Yard and Yard 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 yard System.out.println("Enter value of yard: "); double yard = 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(yard, inch); } //convert() method to convert yard to inch and vice versa public static void convert(double yard, double inch) { //converting inch to yard double y = inch / 36; //converting yard to inch double in = yard * 36; //printing result System.out.println("Value of "+yard+" yard in inch: "+ in); System.out.println("Value of "+inch+" inch in yard: "+ y); } }
Output: Enter value of yard: 4 Enter value of inch: 98765 Value of 4.0 yard in inch: 144.0 Value of 98765.0 inch in yard: 2743.472222222222
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: