divmod() in Python and its application

21 Oct, 2021
The divmod() method in python takes two numbers and returns a pair of numbers consisting of their quotient and remainder. 
Syntax : 
divmod(x, y)
x and y : x is numerator and y is denominator
x and y must be non complex
Examples:  
Input : x = 9, y = 3
Output :(3, 0)

Input : x = 8, y = 3
Output :(2, 2)
Explanation: The divmod() method takes two parameters x and y, where x is treated as numerator and y is treated as the denominator. The method calculates both x // y and x % y and returns both the values. 
If x and y are integers, the return value is
(x // y, x % y)
If x or y is a float, the result is
(q, x % y), where q is the whole part of the quotient.
Python3
# Python3 code to illustrate divmod()# divmod() with intprint('(5, 4) = ', divmod(5, 4))print('(10, 16) = ', divmod(10, 16))print('(11, 11) = ', divmod(11, 11))print('(15, 13) = ', divmod(15, 13)) # divmod() with int and Floatsprint('(8.0, 3) = ', divmod(8.0, 3))print('(3, 8.0) = ', divmod(3, 8.0))print('(7.5, 2.5) = ', divmod(7.5, 2.5))print('(2.6, 10.7) = ', divmod(2.6, 0.5))
Output: 
(5, 4) =  (1, 1)
(10, 16) =  (0, 10)
(11, 11) =  (1, 0)
(15, 13) =  (1, 2)
(6.0, 5) =  (2.0, 2.0)
(3, 9.0) =  (0.0, 3.0)
(13.5, 6.2) =  (3.0, 0.0)
(1.6, 10.7) =  (5.0, 0.10000000000000009)
If either of the arguments (say x and y), is a float, the result is (q, x%y). Here, q is the whole part of the quotient.If the second argument is 0, it returns Zero Division ErrorIf the first argument is 0, it returns (0, 0)
If either of the arguments (say x and y), is a float, the result is (q, x%y). Here, q is the whole part of the quotient.
If the second argument is 0, it returns Zero Division Error
If the first argument is 0, it returns (0, 0)
Practical Application: Check if a number is prime or not using divmod() function.  
Examples: 
Input : n = 7
Output :Prime

Input : n = 15
Output :Not Prime
Algorithm 
Initialise a new variable, say x with the given integer and a variable counter to 0Run a loop till the given integer becomes 0 and keep decrementing it.Save the value returned by divmod(n, x) in two variables, say p and qCheck if q is 0, this will imply that n is perfectly divisible by x, and hence increment the counter valueCheck if the counter value is greater than 2, if yes, the number is not prime, else it is prime
Initialise a new variable, say x with the given integer and a variable counter to 0
Run a loop till the given integer becomes 0 and keep decrementing it.
Save the value returned by divmod(n, x) in two variables, say p and q
Check if q is 0, this will imply that n is perfectly divisible by x, and hence increment the counter value
Check if the counter value is greater than 2, if yes, the number is not prime, else it is prime
PYTHON3
# Python code to find if a number is# prime or not using divmod() # Given integern = 15x = n # Initialising counter to 0count = 0while x != 0:    p, q = divmod(n, x)    x -= 1    if q == 0:        count += 1if count > 2:    print('Not Prime')else:    print('Prime')
Output: 
Not Prime
More Applications: 
Example 1: 
Python3
# Sum of digits of a number using divmodnum = 86sums = 0while num != 0:    use = divmod(num, 10)    dig = use[1]    sums = sums + dig    num = use[0]print(sums)
Output: 
14
Example 2:
Python3
# reversing a number using divmodnum = 132pal = 0while num != 0:    use = divmod(num, 10)    dig = use[1]    pal = pal*10+dig    num = use[0]print(pal)
Output: 
231 
rahul0810
Python-Built-in-functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Convert integer to string in Python
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method