Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remainder Operation
#1
Here's the question and below I will post my response, but I am having a difficult time using the % operation.
4. Write a program named program24.py that converts an improper fraction to a mixed number. Prompt the user for the numerator and the denominator, then calculate and display the equivalent mixed number. Note there should be no space on either side of the / symbol in the output. See sample runs below.

Sample Output 1
Output:
Enter numerator 23 Enter denominator 6 The mixed number is 3 and 5/6
Sample Output 2
Output:
Enter numerator 7 Enter denominator 3 The mixed number is 2 and 1/3
HINT: You should use the // which is integer division and % which is remainder operators, see  page 54 of your textbook
Think you are done and ready to submit? Stop and recheck the requirements. Your professor is your customer and will "pay" you in grade points. "Attention to detail" is an essential skill for programmers."


Response:

numeratorNumber = int(input("What is the numerator? "))
denominatorNumber = int(input("What is the denominator? "))

#Int division of numerator and denominator
mixedNumber = numeratorNumber // denominatorNumber
#print(mixedNumber) Correctly divides 23/6 into 3 for first part

#Remainder operator
leftOver = mixedNumber
float((print(leftOver))//%) 
I get stuck at the remainder operator. I have checked a few online sources and read the textbook for this answer quite a few times, but I just need a little more help with the last bit of coding.

Thank you.
Reply
#2
Small hint

>>> 23%6
5
>>> 7%3
1
>>> 
Do you see the pattern between result of modulo operator (that's the remainder operator) and [part of] the desired output?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020