Python Program to Convert Kilometers to Miles


Levels of difficulty: / perform operation:

Source Code:

# Program to convert kilometers into miles 
# Input is provided by the user in kilometer
# take input from the user
kilometers = float(input('How many kilometers?: '))
# conversion factor
conv_fac = 0.621371
# calculate miles
miles = kilometers * conv_fac
print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles))

Output:

How many kilometers?: 5.5
5.500 kilometers is equal to 3.418 miles


Explanation

In this program, we use the ask the user for kilometers and convert it to miles by multiplying it with the conversion factor. With a slight modification, we can convert miles to kilometers. We ask for miles and use the following formula to convert it into kilometers.

kilometers = miles / conv_fac