Write a program to convert a fraction to a decimal. Have your program ask for the numerator first, then the denominator. Make sure to check if the denominator is zero. If it is, print out "Error - cannot divide by zero. I need the answer to this one.
Answers
Write a program to convert a fraction to a decimal. Have your program ask for the numerator first, then the denominator.
Explanation:
Python program to convert a fraction to a decimal.
num = float(input("Enter value of numerator: "))
denom = float(input("Enter value of denominator: "))
if denom == 0:
print("Error - cannot divide by zero")
# Divide numerator by denominator
else:
print("\n")
result = num/ denom
print("fraction in decimal: " + str(result))
Sample output 1
Enter value of numerator: 7 Enter value of denominator: 2 fraction in decimal: 3.5
Sample output 2
Enter value of numerator: 89 Enter value of denominator: 0 Error - cannot divide by zero