How do you get the remainder program in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m.

    Examples:

    Input:
    n = 10
    m = 3
    Output:
    Quotient:  3
    Remainder 1
    
    Input
    n = 99
    m = 5
    Output:
    Quotient:  19
    Remainder 4

    Method 1: Naive approach

    The naive approach is to find the quotient using the double division (//) operator and remainder using the modulus (%) operator.

    Example:

    Python3

    def find(n, m):

        q = n//m

        print("Quotient: ", q)

        r = n%m

        print("Remainder", r)

    find(10, 3)

    find(99, 5)

    Output:

    Quotient:  3
    Remainder 1
    Quotient:  19
    Remainder 4

    Time Complexity: O(1)

    Auxiliary Space: O(1)

    Method 2: Using divmod() method

    Divmod() method takes two numbers as parameters and returns the tuple containing both quotient and remainder.

    Example:

    Python3

    q, r = divmod(10, 3)

    print("Quotient: ", q)

    print("Remainder: ", r)

    q, r = divmod(99, 5)

    print("Quotient: ", q)

    print("Remainder: ", r)

    Output:

    Quotient:  3
    Remainder 1
    Quotient:  19
    Remainder 4

    Time Complexity: O(1)

    Auxiliary Space: O(1)


    How do you get the remainder program in python?

    Introduction to Python Remainder Operator

    Python remainder operators are used for the computation of some operands. Operators are special symbols that are used on operands to do some operation such as addition, subtraction, division, etc. The operators can be symbolized as ‘+’ for addition, ‘-’ for subtraction, ‘/’ for division, ‘*’ for multiplication, etc. In Python, the modulus operator is a percentage symbol (‘%’) which is also known as python remainder operator, whereas there is a division operator for integer as ’//’, which works only with integer operands also returns remainder but in integers. Similarly, the python remainder operator or modulus operator also returns the remainder when two operands are divided, i.e. one operand is divided with other operand results in us remainder. This remainder operator is used for both integers and float numbers.

    Syntax:

    x % y

    Dividend % Divisor: The remainder is obtained when x is divided by y. The remainder will be an integer if both dividends are integers. The remainder will be a floating-point number if one among dividend or divisor is a float number.

    Examples of Python Reminder Operator

    Following are the different examples of Python Reminder Operator.

    Example #1

    Code:

    x = 5
    y = 2
    r = x % y
    print (‘Remainder is:’, r)

    Output:

    How do you get the remainder program in python?

    Explanation: In the above example x = 5 , y =2 so 5 % 2 , 2 goes into 5 two times which yields 4  so remainder is 5 – 4 = 1. In Python, the remainder is obtained using numpy.ramainder() function in numpy. It returns the remainder of the division of two arrays and returns 0 if the divisor array is 0 (zero) or if both the arrays are having an array of integers. This function is also used on individual numbers.

    Example #2

    Code:

    import numpy as np
    n1 = 6
    n2 = 4
    r = np.remainder(n1, n2)
    print ("Dividend is:", n1)
    print ("Divisor is:", n2)
    print ("Remainder : ", r)

    Output:

    How do you get the remainder program in python?

    Explanation: The above example uses numpy.remainder() function on the given dividend and divisor to find the remains of two, which works similar to the modular operator. In this example, it is 6 % 4, 4 goes into 6, one time which yields 4 so the remainder is 6 – 4 =2.

    Example #3

    Code:

    import numpy as np
    arr1 = np.array([7, -6, 9])
    arr2 = np.array([3, 4, 3])
    rem_arr = np.remainder(arr1, arr2)
    print ("Dividend array is: ", arr1)
    print ("Divisor array is: ", arr2)
    print ("Remainder array is : ", rem_arr)

    Output:

    How do you get the remainder program in python?

    Explanation: In the above example, the numpy.remainder() function can be used on the list of items to calculate the remainder of the respective item in the list or array of elements. we have two arrays [7 -6 9] and [3  4  3], so 7 % 3,3 goes into 7 two times, so the remainder is 1, -6 % 4, 4 goes into 6 one time, so the remainder is 2, 9 % 3, 3 goes into 9 three times, so the remainder is 0. The array of remainder values will be [1  2  0].

    Example #4

    A remainder operator or modulo operator is used to find even or odd numbers. Below is a piece of code to print odd numbers between 0 and 20.

    Code:

    for num in range(1, 20):
        if(num % 2 != 0):
            print(num)

    Output:

    How do you get the remainder program in python?

    Explanation: In the above example using a modulo operator, it prints odd numbers between 0 and 20 from the code; if the number is divided by 2 and the remainder obtained is 0, then we say it as an even number; else its odd number. If the number is 2, then 2 % 2 gives remainder 0, so its an even number, not odd, now; if the number is 3, then 3 % 2 gives remainder 1, which 2 goes into 3 one time so yields 2 and remainder is 3 – 2 =1 which not zero so the given number 3 is odd and using for loop it will check till 20 numbers and print all the odd numbers between 0 and 20. Modulo operator or Remainder operator is also used on floating-point numbers, not like division operator ( // ), which is used only on integers and gives remainder also in integer form.

    Example #5

    Code:

    a = input("Dividend is :\n")
    fa = float(a)
    b = input("Divisor is :\n")
    fb = float(b)
    fr = fa % fb
    print ("Remainder is",fr)

    Output:

    How do you get the remainder program in python?

    Example #6

    In Python, the modulo operator can be used on negative numbers also which gives the same remainder as with positive numbers, but the negative sign of the divisor will be the same in the remainder.

    Code:

    print(-5 % 3)

    Output:

    How do you get the remainder program in python?

    Code:

    print(5 % -3)

    Output:

    How do you get the remainder program in python?

    Logic Behind the Code:

    • -5 % 3 = (1 -2*3) % 3 = 1
    • 5 % -3 = (-1 * -2*-3) % 3 = -1

    Explanation: These negative numbers use the fmod() function to find the remainder; if any one of the numbers among dividend or divisor is negative, then we can even use the fmod() function of the math library, and this can also be used to find the remainder of floating-point numbers also.

    Example #7

    Code:

    import math
    a = -10
    b = 3
    print(math.fmod(a,b))

    Output:

    How do you get the remainder program in python?

    Explanation: In Python, the modulo operator gives an error when the divisor is zero (0). Usually, it gives ZeroDivisionError as we know any number divided by zero is infinity (∞).

    Example #8

    Code:

    p = 10
    q = 0
    r = p % q
    print(r)

    The above code gives us an error, as shown in the below screenshot.

    Output:

    How do you get the remainder program in python?

    Code:

    p = 10
    q = 0
    try:
        rem = p * q
        print(rem)
    except ZeroDivisionError as zde:
        print("Cannot divided by 0")

    This error can be caught by using try-except blocks, as shown in the below screenshot.

    Output:

    How do you get the remainder program in python?

    Conclusion

    In Python, the modulo operator is the operator to obtain the remainder of the division of two numbers known as dividend and divisor. This operator can be used to find the remainder of both integer and float data type numbers. The modulo operator is also one among the mathematical operators like addition ( +) , subtraction (- ), division( // ), etc. Division operator is used only on integer numbers, unlike modulo operator. And if the divisor is zero, we can handle it by exception handling using a try-except block to print the error.

    How do you find the remainder in a program?

    The remainder is obtained by using the modulus operator on dividend and divisor. quotient = dividend / divisor; remainder = dividend % divisor; After that the dividend, divisor, quotient and remainder are displayed.

    How do you get the remainder and divisor in Python?

    In Python, you can calculate the quotient with // and the remainder with % ..
    q = 10 // 3 mod = 10 % 3 print(q, mod) # 3 1. source: divmod-test.py..
    q, mod = divmod(10, 3) print(q, mod) # 3 1. source: divmod-test.py..
    answer = divmod(10, 3) print(answer) print(answer[0], answer[1]) # (3, 1) # 3 1. source: divmod-test.py..

    What does the remainder mean in Python?

    The modulus operator, sometimes also called the remainder operator or integer remainder operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign ( % ). The syntax is the same as for other operators.

    How do you print the remainder of two numbers in Python?

    Python3. Divmod() method takes two numbers as parameters and returns the tuple containing both quotient and remainder.