November 3, 2017
Python integer division with remainder
Refer to the following post for more details. This post is only Python implementation
Python code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# Integer division calculator def Division(numerator, denominator): # Divide by zero special case if denominator == 0: return [-999, -999] # Both are equal special case if numerator == denominator: return [1, 0] quotient = 0 # Get the sign of the division sign = 1 if numerator * denominator < 0: sign = -1 # Convert to positive or you can use # built in ABS function if numerator < 0: numerator = -1 * numerator if denominator < 0: denominator = -1 * denominator while numerator > denominator: quotient = quotient + 1 numerator = numerator - denominator return [sign * quotient, numerator] # Give it a try result = Division(9, 2) print "9/2 = {} and remainder = {}".format(result[0], result[1]) |
Thanks for reading, for questions or feedback please use the comments section below.
More from my site
3 Comments
Mr Mohammed i am having major problem in doing this hours and rate per hours. I am told to do integer division for hours worked and remainder division for the overtime. I am also told to put 45 hours worked at $10.50 pay rate. The answer should be 498.75 but I keep getting another number instead. please help. Thank you
#Named constant
baseHours = 45
overtimerate = 1.5
overtime = baseHours – 40
hourlyRate = 10.5
#Get input from user
totalHours = float(input(“Enter housrs worked: “))
hourlyRate = float(input(“Enter pay rate: “))
#Calculate gross pay
if totalHours > baseHours:
grossPay = totalHours * hourlyRate
#Calculate total hours using Integer Division
totalHours = grossPay//hourlyRate
#Calculate overtime hours using Remainder Division
overtimeHours = grossPay%totalHours
#Calculate gross pay with overtime
grossPay = (baseHours * hourlyRate)+ (overtimeHours * hourlyRate * overtimerate)
else:
#Calculate gross pay without overtime
grossPay = hourlyRate * totalHours
#Display gross pay
print(“The gross pay is $: “, format(grossPay, ‘,.2f’))
The answer to this program is 498.75
please correct the area that is incorrect so I can get the correct answer to this program. I have tried everything that my knowledge permits.