Python Program to Convert Kilometers to Miles

Python Program to Convert Kilometers to Miles
Reading Time: 3 minutes

Introduction:

This Python program will convert kilometers to miles. It will take a distance in kilometers as input from the user and then convert it to miles using the conversion formula. The program will display the converted distance in miles as output.

Algorithm:

  1. Read the distance in kilometers from the user.
  2. Perform the conversion using the formula: miles = kilometers * 0.621371.
  3. Display the converted distance in miles.

Python code:

    
     # Introduction
print("Kilometers to Miles Converter")
print("-----------------------------")
# Read the distance in kilometers from the user
kilometers = float(input("Enter distance in kilometers: "))
# Perform the conversion
miles = kilometers * 0.621371
# Display the converted distance in miles
print("Distance in miles:", miles)

    
   

Input:

    
     Enter distance in Kilometres : 10
    
   

output:

    
     Distance in miles : 6.21371
    
   

Explanation :

In the above program, we prompt the user to enter the distance in kilometers. The input function is used to read the user’s input, which is then converted to a floating-point number using the float function.

Next, we perform the conversion by multiplying the distance in kilometers by the conversion factor of 0.621371, which represents the number of miles in one kilometer.

Finally, we display the converted distance in miles using the print function. The output is formatted to two decimal places for better readability.

For example, if the user enters a distance of 10 kilometers, the program will output the converted distance as 6.21371 miles.