Introduction:
This Python code allows users to input a numerator and denominator and checks if it forms an improper fraction. If it is an improper fraction, the code calls a function called mixedFraction() to display the mixed fraction. If the fraction evaluates to a whole number, it will display a message indicating that.
Code:
#Function to display mixed fraction for an improper fraction
#The requirements are listed below:
#1. Input numerator and denominator from the user.
#2. Check if the entered numerator and denominator form a proper fraction.
#3. If they do not form a proper fraction, then call mixedFraction().
#4. mixedFraction()display a mixed fraction only when the fraction does not evaluate to a whole number.
def mixedFraction(num,deno = 1):
remainder = num % deno
#check if the fraction does not evaluate to a whole number
if remainder!= 0:
quotient = int(num/deno)
print("The mixed fraction=", quotient,"(",remainder, "/", deno,")")
else:
print("The given fraction evaluates to a whole number")
#function ends here
num = int(input("Enter the numerator: "))
deno = int(input("Enter the denominator: "))
print("You entered:",num,"/",deno)
if num > deno: #condition to check whether the fraction is improper
mixedFraction(num,deno) #function call
else:
print("It is a proper fraction")
Logic:
- Get the numerator and denominator from the user.
- Check if the entered fraction is an improper fraction (num > deno).
- If it is an improper fraction, call the mixedFraction() function.
- In the mixedFraction() function:
– Calculate the remainder of the fraction (num % deno).
– If the remainder is not 0, calculate the quotient (num/deno) and print the mixed fraction as “The mixed fraction = quotient (remainder/denominator)”.
– If the remainder is 0, print “The given fraction evaluates to a whole number”. - If the fraction is not an improper fraction, print “It is a proper fraction”.
Output:
The output of this code will be the display of the entered fraction and whether it is a proper fraction or an improper fraction. If it is an improper fraction, the mixed fraction will be displayed.
Enter the numerator: 10
Enter the denominator: 7
>> You entered: 10 / 7
>> The mixed fraction= 1 ( 3 / 7 )