Python: Convert feet and inches to centimeters
Python Basic: Exercise-59 with Solution
Height to Centimeters
Write a Python program to convert height (in feet and inches) to centimeters.
Sample Solution:
Python Code:
# Prompt the user to input their height.
print("Input your height: ")
# Read the feet part of the height and convert it to an integer.
h_ft = int(input("Feet: "))
# Read the inches part of the height and convert it to an integer.
h_inch = int(input("Inches: "))
# Convert the height from feet and inches to inches.
h_inch += h_ft * 12
# Calculate the height in centimeters by multiplying by the conversion factor (2.54).
h_cm = round(h_inch * 2.54, 1)
# Print the calculated height in centimeters.
print("Your height is : %d cm." % h_cm)
Sample Output:
Input your height: Feet: 5 Inches: 3 Your height is : 160 cm.
Explanation:
h_ft = int(input("Feet: ")) h_inch = int(input("Inches: "))
In the above code -
The input() function is used to prompt the user to enter their height in feet and inches, and the int() function is used to convert the input to integer values for h_ft (height in feet) and h_inch (height in inches).
h_inch += h_ft * 12: The height in inches is then calculated by adding h_ft * 12 to h_inch, which converts the height to inches.
h_cm = round(h_inch * 2.54, 1): The height in centimeters is calculated by multiplying the height in inches by 2.54 (the conversion factor from inches to centimeters), and rounding the result to one decimal place using the round() function.
Finally, the print() function prints a message to the console in the format "Your height is : height_cm cm".
Flowchart:
Python Code Editor:
Previous: Write a Python program to sum of the first n positive integers.
Next: Write a Python program to calculate the hypotenuse of a right angled triangle.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/python-basic-exercise-59.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics