Python program to enter the length in centimeter and convert it into meter and kilometer. There are you will learn how to convert the centimeter into meter and kilometer in Python language.
Q: Write a program to Enter length in centimeters and convert it into meter and kilometer
Formula:
1m = 100cm
1km = 100000cm
Write a program to convert kilometers to meters in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # Python program to convert centimeter into meter and kilometer print("Enter the length in centimeter::") c, m, k = float(input()), 0, 0 # c = centimeter # m = meter # k = kilometer # Convert centimeter into meter and kilometer m = (float)(c / 100) k = (float)(c / 100000) # Output print("Length in Meter = ", m, " meter") print("Length in Kilometer = ", k, " kilometer") |
Output:
1 2 3 4 5 6 7 8 9 | Enter the length in centimeter:: 100 Length in Meter = 1.0 meter Length in Kilometer = 0.001 kilometer |
What happend in this code:
- The first line
print("Enter the length in centimeter::")
is used to prompt the user to enter a length in centimeter. - The line
c, m, k = float(input()), 0, 0
is used to take the input from the user and store it in a variablec
, and initialize variablesm
andk
to 0. - The line
m = (float)(c / 100)
converts the length in centimeter to meter by dividing it by 100, and storing the result in them
variable. - The line
k = (float)(c / 100000)
converts the length in centimeter to kilometer by dividing it by 100,000, and storing the result in thek
variable. - Finally, the lines
print("Length in Meter = ", m, " meter")
andprint("Length in Kilometer = ", k, " kilometer")
are used to print the length in meter and kilometer, respectively.
That’s it! This simple program takes a length in centimeter as input from the user and converts it into meter and kilometer.