In the previous article we have discussed about Java Program to Convert Kilometer Per Hour to Meters Per Second
In this article we are going to see how to convert kilometer per hour to miles per hour by using Java programming language.
Java Program to Convert Kilometer Per Hour to Miles Per Hour
Kilometer and Mile both are used as a unit to measure length of long distance like road distance.
Before jumping into the program, first let’s know the relation between kilometer per hour and miles per hour.
1 Kilometer Per Hour = 0.621371 Miles Per Hour
Then the formula to convert kilometer per hour to miles per hour is
Miles Per Hour = Kilometer Per Hour * 0.621371
Let’s see different ways to convert kilometer per hour to miles per hour.
Method-1: Java Program to Convert Kilometer Per Hour to Miles Per Hour By Using Static Input Value
Approach:
- Declare a double variable say ‘kilometerPerHour‘ and initialize it’s value.
- Then convert kilometer per hour to miles per hour by using formula.
- Print the result.
Program:
public class Main { public static void main(String[] args) { //declared value of kilometer per hour double kilometerPerHour=1; //converting kilometer per hour to miles per hour double milesPerHour=kilometerPerHour * 0.621371; //printing result System.out.println(kilometerPerHour+" kilometer per hour is equal to "+milesPerHour+" miles per hour"); } }
Output: 1.0 kilometer per hour is equal to 0.621371 miles per hour
Method-2: Java Program to Convert Kilometer Per Hour to Miles Per Hour By Using User Input Value
Approach:
- Take the value of double variable ‘kilometerPerhour‘ as user input by using Scanner class.
- Then convert kilometer per hour to miles per hour by using formula.
- Print the result.
Program:
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the value of kilometer per hour: "); //declared double variable kilometerPerHour //and taking it's value as user input double kilometerPerHour=sc.nextDouble(); //converting kilometer per hour to miles per hour double milesPerHour=kilometerPerHour * 0.621371; //printing result System.out.println(kilometerPerHour+" kilometer per hour is equal to "+milesPerHour+" miles per hour"); } }
Output: Enter the value of kilometer per hour: 4 4.0 kilometer per hour is equal to 2.485484 miles per hour
Method-3: Java Program to Convert Kilometer Per Hour to Miles Per Hour By Using User Defined Method
Approach:
- Take the value of double variable ‘kilometerPerHour‘ as user input by using Scanner class.
- Then call a user defined method say
convertValue()
by passing kilometerPerHour value as parameter. - Inside method convert kilometer per hour to miles per hour by using formula.
- Print the result.
Program:
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the value of kilometer per hour: "); //declared double variable kilometerPerHour //and taking it's value as user input double kilometerPerHour=sc.nextDouble(); //calling user defined method converValue() convertValue(kilometerPerHour); } //convertValue() user defined method public static void convertValue(double kilometerPerHour) { //converting kilometer per hour to miles per hour double milesPerHour=kilometerPerHour * 0.621371; //printing result System.out.println(kilometerPerHour+" kilometer per hour is equal to "+milesPerHour+" miles per hour"); } }
Output: Enter the value of kilometer per hour: 10 10.0 kilometer per hour is equal to 6.21371 miles per hour
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: