Project: Proper Fractions, Improper Fractions, and Mixed Fractions
Problem Description
Proper fractions, improper fractions, and mixed fractions are defined at http://www.ltcconline.net/greenl/courses/187/b/impropermixed.htm. Write a program that prompts the user to enter the numerator and denominator of a fraction number and determines whether it is a proper fraction and improper fraction. For an improper fraction number, display its mixed fraction in the form of a + b / c if b % c is not zero; otherwise, display only the integer.
Here are sample runs of the program
Sample 1
Enter a numerator: 16
Enter a denominator: 3
16 / 3 is an improper fraction and its mixed fraction is 5 + 1 / 3.
Sample 2
Enter a numerator: 6
Enter a denominator: 7
6 / 7 is a proper fraction
Sample 3
Enter a numerator: 6
Enter a denominator: 2
6 / 2 is an improper fraction and it can be reduced to 3
Solution:
Here is the code for the above requirement in Python 3 programming language:
if __name__ == "__main__":
# Take inputs from user
numerator = int(input("Enter a numerator: "))
denominator = int(input("Enter a denominator: "))
# To avoid the divide by 0 error
if denominator == 0:
print('Denominator cannot be 0')
# 0 divided by any number is 0
elif numerator == 0:
print('0/{} is a proper fraction with value
0'.format(denominator))
else:
if numerator == denominator:
print('{}/{} is a proper fraction with value
1'.format(numerator,denominator))
elif numerator < denominator:
print('{}/{} is a proper
fraction'.format(numerator,denominator))
else:
# divmod() is an in-built function which takes input in the form of
numerator and denominator and returns the quotient and
remainder
quotient,remainder = divmod(numerator,denominator)
if remainder == 0:
print('{}/{} is an improper fraction and it can be reduced to
{}'.format(numerator,denominator,quotient))
else:
print('{}/{} is an improper fraction and its mixed fraction is {}
+{}/{}'.format(numerator,denominator,quotient,remainder,denominator))
Get Answers For Free
Most questions answered within 1 hours.