Replace divmod() by for loop to get quotient and remainder

1.4k views Asked by At

I am a newbie in python, idk how to do the following question, please help.....

Many programming languages implement a function called divmod() (short for division and modulo), which returns both the quotient and the remainder when dividing two positive numbers using floor division.

The relationship among the 4 numbers can be described by the following formula:

=×+

Write a program to take inputs for the dividend and divisor of a division from user, and use a for loop to find the corresponding quotient and remainder without using /, *, //, %, and divmod(). In other words, you are only allowed to use the operators for addition and subtraction in your code.

Assume that you input 45 for the dividend and 7 for the divisor, print your result as following:

Input a positive integer for the dividend: 45

Input a positive integer for the divisor: 7

45 divided by 7 yields:

quotient: 6

remainder: 3

The code should work correctly if one entered another sets of value.

3

There are 3 answers

0
jferard On

Here's an answer using a while loop.

>>> dividend = 45
>>> divisor = 7

You have dividend cows and divisor cow-boys (don't be offended by the childish example: this is a wink to my elder son...). At the beginning, all cows are in the corral.

>>> remainder = dividend

And you haven't given any cow to any cow-boy.

>>> quotient = 0

Now, do an euclidean division: while you can give one cow to every cow-boy...

>>> while remainder >= divisor:
...     remainder -= divisor # remove those `divisor` cows from the corral...
...     quotient += 1        # ...and note that you have given one more cow to each cow-boy

The result is:

>>> quotient, remainder
(6, 3)

Hope it's clear!


EDIT about the for loop

If you absolutely need to use a for, you can emulate a while using a break.

We assume that dividend and divisor >= 1. Hence, quotient <= divisor.

>>> remainder = dividend
>>> quotient = 0

>>> for _ in range(dividend):
...     if remainder < divisor:
...         break
...     quotient += 1
...     remainder -= divisor

>>> quotient, remainder
(6, 3)

If break is not allowed, then you can use a hack (I don't recommend this: the break is simply hidden into the iterator):

>>> remainder = dividend
>>> quotient = 0

>>> for _ in iter(lambda: remainder >= divisor, False):
...     quotient += 1
...     remainder -= divisor

>>> quotient, remainder
(6, 3)

You could also use a recursive function:

>>> def divm(divid, divis):
...     if divid < divis:
...         return 0, divid
...     else:
...         quotient_minus_one, remainder = divm(divid - divis, divis)
...         return quotient_minus_one + 1, remainder

>>> divm(dividend, divisor)
(6, 3)
0
Richestmanintheworld On

div = int(input('Input a positive interger for the dividend: ')) #dividend

dir = int(input('Input a positive integer for the divisor: ')) #divisor

def mod(div, dir):

quotient = 0 #counter variable

for dir in range(div + 1):#dir <= div

    quotient = div - dir

    div -= dir

return quotient

remain = quotient

print(div ," divided by" ,dir ,"yields:" , "\nquotient: " , quotient , "\nremainder: " ,remain)

This is my answer but not working to find quotient and remainder :(

0
Coder On

Try making a loop and subtracting until you hit a certain number, also have a counter variable. The number of times you iterate aka your counter variable will be your quotient.

I'll do mod for you:

def mod(a, b):
  while b <= a:
    c = a - b
    a -= b
  return c

>>> mod(45, 7)
3

But I gave you the logic for quotient/floordiv, try it yourself!