Programs · Program 7-9
Q.Program 7-9: Write a program that accepts numerator and denominator of a fractional number and calls a user defined function mixedFraction() when the fraction formed is not a proper fraction. The default value of denominator is 1. The function displays a mixed fraction only if the fraction formed by the parameters does not evaluate to a whole number.
Puducherry TnboardTextbookSubjective· 3mImportance★★★★★est
28% · 11/40 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Program 7-9 demonstrates a default parameter value (deno=1) — the main program calls mixedFraction() only for improper fractions, and the function prints quotient + remainder/denominator unless the fraction reduces to a whole number.
The idea. Two Chapter-7 features meet here: a default parameter (if the caller omits the denominator it is taken as 1) and the division operators — // (integer quotient) and % (remainder) — which decompose an improper fraction into its mixed form: num/deno = (num // deno) + (num % deno)/deno.
# Program 7-9: display an improper fraction as a mixed fraction
def mixedFraction(num, deno=1):
"""Display num/deno as a mixed fraction, unless it is a whole number."""
if num % deno == 0:
print("The fraction evaluates to the whole number", num // deno)
else:
quotient = num // deno
remainder = num % deno
print("Mixed fraction:", quotient, "+", str(remainder) + "/" + str(deno))
num = int(input("Enter the numerator: "))
deno = int(input("Enter the denominator: "))
if num >= deno: # improper fraction (not proper)
mixedFraction(num, deno)
else:
print("It is a proper fraction")
Key lines
def mixedFraction(num, deno=1)—denohas a default value;mixedFraction(7)would use deno = 1 (7/1, a whole number). Parameters with defaults must come after those without.num % deno == 0— remainder zero means the fraction is a whole number, so no mixed fraction is shown (exactly what the question requires). …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.