Distance Calculation in Python


The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.

This program is a simple Python script that converts kilometers to various other units of length, such as meters, centimeters, inches, and feet. It prompts the user to input a distance in kilometers, converts that distance to the other units using mathematical operations, and then prints out the results.

The program first prompts the user to input a distance in kilometers, which is stored as the variable "km" by using the input() function. The float() function is used to convert the input from a string to a floating point number.

Next, the program converts kilometers to meters by multiplying the input by 1000 and assigns the value to a variable "m", it then converts meters to centimeters by multiplying the value of "m" by 100, assign it to a variable "cm". The program then converts centimeters to inches by dividing the value of "cm" by 2.54, assign it to a variable "i" and then convert inches to feet by dividing the value of "i" by 12 and assigns the value to a variable "ft".

Finally, the program uses the print() function to output the results, including the original input in kilometers, the converted values in meters, centimeters, inches, and feet.


Source Code

'''
Formulas:
meters = kilometers × 1000
centimeters = meters × 100
inches = centimeters ÷ 2.54
feet = inches ÷ 12
'''
km=float(input("Enter The Kilometer : "));
m=km*1000;
cm=m*100;
i=cm/2.54;
ft=i/12;
print("Kilometers : ",km);
print("Meters  : ",m);
print("Centimeters : ",cm);
print("Inches : ",i);
print("Feet : ",ft);
 

Output

Enter The Kilometer : 12
Kilometers :  12.0
Meters  :  12000.0
Centimeters :  1200000.0
Inches :  472440.94488188974
Feet :  39370.07874015748


Example Programs