Python program to convert kilometers to miles

Published by admin on

Python kilometer to mile conversion

Convert Kilometer :

In this python programming tutorial, we will learn how to convert a kilometre value to miles. The program will take the kilometre input from the user, convert it and print out the result.


Algorithm :

The following algorithm we will use :

  1. Ask the user to enter the kilometre value.
  2. 1 kilometre is approx 0.621371 miles. So, multiply the user input value with 0.621371 to convert it to miles.
  3. Print out the result to the user.
  4. Exit the program.


Python program :

fact = 0.621371

km = float(input("Enter the kilometre value : "))
miles = km * fact 

print("{} km = {} miles".format(km,miles))

python convert kilometre miles


Explanation :

As you can see, we are reading the kilometre value as float and calculated the value of the miles by multiplying it with fact or 0.621371.
Finally, we are printing the result.


Sample Output :

Enter the kilometre value : 3.5
3.5 km = 2.1747985 miles

Enter the kilometre value : 2
2.0 km = 1.242742 miles

Enter the kilometre value : 10
10.0 km = 6.21371 miles

Enter the kilometre value : 100
100.0 km = 62.137100000000004 miles

Enter the kilometre value : 40
40.0 km = 24.85484 miles


Conclusion :

In this tutorial, we have learned how to convert kilometre value to mile. You can also change this program to convert a mile value to kilometre. Try to run the program and drop one comment below if you have any queries.

Categories: python


Leave a Reply

Your email address will not be published. Required fields are marked *