Java Program to Convert Miles Per Hour to Kilometer Per Hour

In the previous article we have discussed about Java Program to Convert Miles Per Hour to Feet Per Second

In this article we are going to see how to convert miles per hour to kilometers per hour by using Java programming language.

Java Program to Convert Miles Per Hour to Kilometer Per Hour

Both Miles and Kilometers are used as a unit of length to measurement mainly when the length is high like road distance measurement..

Before jumping into the program, first let’s know the relation between miles per hour and kilometers per hour.

1 Miles Per Hour = 1.60934 Kilometers Per Hour

Then the formula to convert miles per hour to kilometers per hour is

Kilometers Per Hour = Miles Per Hour * 1.60934

Let’s see different ways to convert miles per hour to kilometers per hour.

Method-1: Java Program to Convert Miles Per Hour to Kilometer Per Hour By Using Static Input Value

Approach:

  • Declare a double variable say ‘kilometerPerHour‘ and initialize it’s value.
  • Then convert miles per hour to kilometers per hour by using formula.
  • Print the result.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //declared value of miles per hour
        double milesPerHour=1;
        //converting miles per hour to kilometer per hour
        double kilometerPerHour=milesPerHour*1.60934;
        //printing result
        System.out.println(milesPerHour+" miles per hour is equal to "+kilometerPerHour+" kilometer per hour");
    }
}
Output:

1.0 miles per hour is equal to 1.60934 kilometer per hour

Method-2: Java Program to Convert Miles Per Hour to Kilometer Per Hour By Using User Input Value

Approach:

  • Take the value of double variable ‘kilometerPerHour‘ as take its’ value user input.
  • Then convert miles per hour to kilometers 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 miles per hour: ");
        //declared double variable milesPerHour
        //and taking it's value as user input
        double milesPerHour=sc.nextDouble();
        //converting miles per hour to kilometer per hour
        double kilometerPerHour=milesPerHour*1.60934;
        //printing result
        System.out.println(milesPerHour+" miles per hour is equal to "+kilometerPerHour+" kilometer per hour");
    }
}
Output:

Enter the value of miles per hour: 5
5.0 miles per hour is equal to 8.0467 kilometer per hour

Method-3: Java Program to Convert Miles Per Hour to Kilometer Per Hour By Using User Defined Method

Approach:

  • Take the value of double variable ‘kilometerPerHour‘ as take its’ value user input.
  • Then call a user defined method say convertValue() by passing kilometerPerHour value as parameter.
  • Then convert miles per hour to kilometers 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 miles per hour: ");
        //declared double variable milesPerHour
        //and taking it's value as user input
        double milesPerHour=sc.nextDouble();
        //calling user defined method converValue()
        convertValue(milesPerHour);
    }
    
    //convertValue() user defined method 
    public static void convertValue(double milesPerHour)
    {
        //converting miles per hour to kilometer per hour
        double kilometerPerHour=milesPerHour*1.60934;
        //printing result
        System.out.println(milesPerHour+" miles per hour is equal to "+kilometerPerHour+" kilometer per hour");
    }
}
Output:

Enter the value of miles per hour: 17.7
17.7 miles per hour is equal to 28.485318 kilometer 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: