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 the numerator and y — as the denominator. The method evaluates both x / y and x% y and returns both.
# Python3 illustration code divmod () # divmod () with int print ( ’(5, 4) =’ , divmod ( 5 , 4 )) print ( ’(10, 16) =’ < / code> , divmod ( 10 , 16 )) print ( ’(11, 11) =’ , divmod ( 11 , 11 )) print ( ’(15, 13) =’ , divmod ( 15 , 13 )) # divmod () with int and Floats print ( ’ (6.0, 5) = ’ , divmod ( 8.0 , 3 )) print ( ’(3, 9.0) =’ , divmod ( 3 , 8.0 )) print ( ’(13.5, 6.2) =’ , divmod ( 7.5 , 2.5 )) print ( ’ (1.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 )
Errors and Exceptions
- If any of the arguments, say x and y, are floating point, the result is (q, x% y). Here q — whole part of the quotient.
- If the second argument is 0, it returns a division by zero error
- If the first argument is 0, it returns (0, 0)
Practical Application: Check if the number is prime or not using the divmod () function.
Examples :
Input: n = 7 Output: Prime Input: n = 15 Output: Not Prime
Algorithm
- Initialize a new variable, say x with the given integer and the variable counter is 0
- Run the loop until the given integer is 0 and keep decrementing its.
- Store the value returned by divmod (n, x) into two variables, say p and q
- Check if q is 0, this would mean n is fine is divisible by x, and therefore increase the counter value
- Make sure the counter value is greater than 2, if yes, the number is not prime, otherwise it is prime
# Python code to find if a number is # simple or not using divmod () # Y ano integer n = 15 x = n # Initialize counter to 0 count = 0 while x! = 0 : p, q = divmod (n, x) x - = 1 if < / code> q = = 0 : count + = 1 if count" 2 : print ( ’Not Prime’ ) else : print ( ’Prime’ ) |
Output:
Not Prime
More Apps:
# Sum of digits of a number using divmod num = 86 sums = 0 while num! = 0 : use = divmod (num, 10 ) dig = use [ 1 ] < / code> sums = sums + dig num = use [ 0 ] print (sums) |
Output:
14
# change the number with divmod num = 132 pal = 0 while num! = 0 : use = divmod (num, 10 ) dig = use [ 1 ] pal = pal * 10 + dig num = use [ 0 ] print (pal) |
Output:
231
Co-author: Chinmoy Lenka
Shop
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best laptop for development
$499+
Best laptop for Cricut Maker
$299+
Best laptop for hacking
$890
Best laptop for Machine Learning
$699+
Raspberry Pi robot kit
$150
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically