Python Program to Compute Quotient and Remainder
- More than 40 Lakhs Students use Grammarly for their spell and grammar checks. You can register and try for free.
In this tutorial, you will learn how to compute the quotient and the reminder using the input function, format function, print function, the double division and modulus operator of the python programming language.
How to Compute the Quotient and Reminder in Python?
Let’s take a look at the first source code , here the values are assigned in the code, the double division and modulus operator carry out the function.
# Python program to compute the quotient and the reminder def find(n, m): # for quotient q = n//m print("The quotient is:", q) # for remainder r = n%m print("The remainder is:", r) # Driver Code find(14, 5) find(69, 9)
The quotient is: 2 The remainder is: 4 The quotient is: 7 The remainder is: 6
- At the start, we use
def find(n, m)where the
defkeyword is used to define a function and the
findfunction is used to find the first occurrence of the specified value.
- Declare the formula to compute the quotient as
q = n//m, where the double division operator is used to compute and return the quotient. The value is displayed with the statement
("The quotient is:", q)using the
- Similarly, declare the formula to compute the reminder as
r = n%m, where the modulus operator is used to compute and return the reminder. The value is displayed with the statement
("The reminder is:", r)using the
- The input values
(n, m)are entered in the code within the
Let’s take a look at the second source code , here the values are assigned in the code and we use the divmod function to compute the quotient and reminder.
# Python program to find the quotient and remainder using divmod() q, r = divmod(14, 5) print("Quotient: ", q) print("Remainder: ", r) q, r = divmod(69, 9) print("Quotient: ", q) print("Remainder: ", r)
Quotient: 2 Remainder: 4 Quotient: 7 Remainder: 6
- At the start, we declare the variables
q, rwith the function
divmodalong with the input values
(14, 5). We display the statements with the
- Similarly in the second part of the code, the same steps are followed with different input values
(69, 9)with the statements displayed with the
divmod()function takes two numbers and returns a pair of numbers consisting of their quotient and remainder. To explain with an example – The
divmod()function takes two parameters x and y, where x is treated as numerator and y is treated as the denominator. The function calculates both x / y and x % y and returns both the values.
Let’s take a look at the third source code , here the values are given as input by the user in the code, the double division and modulus operator carries out the function.
#Program to compute the quotient and the reminder n =int(input("Enter the dividend: ")) d =int(input("\nEnter the divisor: ")) #To compute the quotient and reminder quotient=n // d; remainder=n % d; #To display the values print("\nQuotient is: ",quotient) print("remainder is: ",remainder)
Enter the dividend: Enter the divisor: Quotient is: 16 remainder is: 2
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction which is declared with the variable the variables
dfor the dividend and the divisor.
- We use
int()function before the input function which converts the specified value into an integer number.
- In the STDIN section of the code editor the input values are entered.
- We declare the operators
%with the variables
dwhich computes the quotient and the reminder of the input values. The values are saved in the variables
- Then we display the output value using the
- The double division operator (//) is used to compute the quotient and the modulus operator (%) is used to compute the reminder.
- The int() function is used to convert the specified value into an integer number and evaluates the input and accordingly appropriate data type is returned.
- The divmod() method in python takes two numbers and returns a pair of numbers consisting of their quotient and remainder.
- The print statement/string to be displayed in enclosed in double quotes.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.