In this python 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.
RUN CODE SNIPPET# 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)
OUTPUT:
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 thedef
keyword is used to define a function and thefind
function 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 theprint
function which followed by a comma and the declared variableq
. - 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 theprint
function which followed by a comma and the declared variabler
. - The input values
(n, m)
are entered in the code within thefind
function.
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.
RUN CODE SNIPPET# 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)
OUTPUT:
Quotient: 2 Remainder: 4 Quotient: 7 Remainder: 6
- At the start, we declare the variables
q, r
with the functiondivmod
along with the input values(14, 5)
. We display the statements with theprint
function followed by the declared variablesq
andr
. - Similarly in the second part of the code, the same steps are followed with different input values
(69, 9)
with the statements displayed with theprint
function. - The
divmod()
function takes two numbers and returns a pair of numbers consisting of their quotient and remainder. To explain with an example – Thedivmod()
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.
RUN CODE SNIPPET#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)
INPUT:
50 3
OUTPUT:
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
input
function which is declared with the variable the variablesn
andd
for 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
//
and%
with the variablesn
andd
which computes the quotient and the reminder of the input values. The values are saved in the variablesQuotient
andReminder
- Then we display the output value using the
print
function along with the statement/string followed by the declared value,Quotient
andReminder
.
NOTE:
- 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.