In the previous article we have discussed about Java Program to Convert Kilometer to Nautical Mile and Nautical Mile meter to Kilometer
In this article we will see how to convert Kilometer to Yard and Yard to Kilometer by using Java programming language.
Java Program to Convert Kilometer to Yard and Yard to Kilometer
Before jumping into the program let’s know the relationship between Kilometer and Yard and how we can convert Kilometer to Yard and vice versa.
Generally Kilometer and Yard are used as unit in case of distance measurement.
1 Kilometer = 1093.61 Yard 1 Yard = 0.0009144 Kilometer
(For an approximate result)
Formula to convert Kilometer to Yard.
Yard = Kilometer * 1094
Formula to convert Yard to Kilometer.
Kilometer = Yard * 0.0009144
Let’s see different ways to convert Kilometer to Yard and Yard to Kilometer.
Method-1: Java Program to Convert Kilometer to Yard and Yard to Kilometer By Using Static Input Value
Approach:
- Declare Kilometer and Yard value.
- Then convert Kilometer to Yard and Yard 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 yard declared double yard = 1; //value of kilometer declared double kilometer = 1; //converting kilometer to yard double yd = kilometer*1094; //converting yard to kilometer double km = yard/1094; //printing result System.out.println("Value of "+yard+" yard in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in yard: "+ yd); } }
Output: Value of 1.0 yard in kilometer: 9.140767824497258E-4 Value of 1.0 kilometer in yard: 1094.0
Method-2: Java Program to Convert Kilometer to Yard and Yard to Kilometer By Using User Input Value
Approach:
- Take user input of Kilometer and Yard value.
- Then convert Kilometer to Yard and Yard 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 yard System.out.println("Enter value of yard: "); double yard = sc.nextDouble(); //Taking the value input of double variable kilometer System.out.println("Enter value of kilometer: "); double kilometer = sc.nextDouble(); //converting kilometer to yard double yd = kilometer*1094; //converting yard to kilometer double km = yard/1094; //printing result System.out.println("Value of "+yard+" yard in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in yard: "+ yd); } }
Output: Enter value of yard: 2000000 Enter value of kilometer: 2 Value of 2000000.0 yard in kilometer: 1828.1535648994516 Value of 2.0 kilometer in yard: 2188.0
Method-3: Java Program to Convert Kilometer to Yard and Yard to Kilometer By Using User Defined Method
Approach:
- Take user input of Kilometer and Yard value.
- Call a user defined method by passing Kilometer and Yard value as parameter.
- Inside method convert Kilometer to Yard and Yard 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 yard System.out.println("Enter value of yard: "); double yard = 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(yard, kilometer); } //convert() method to convert yard to kilometer and vice versa public static void convert(double yard, double kilometer) { //converting kilometer to yard double yd = kilometer*1094; //converting yard to kilometer double km = yard/1094; //printing result System.out.println("Value of "+yard+" yard in kilometer: "+ km); System.out.println("Value of "+kilometer+" kilometer in yard: "+ yd); } }
Output: Enter value of yard: 9999000 Enter value of kilometer: 10 Value of 9999000.0 yard in kilometer: 9139.853747714807 Value of 10.0 kilometer in yard: 10940.0
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: