HomePythonPython Program to Compute Quotient and Remainder

Python Program to Compute Quotient and Remainder

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
  1. At the start, we use def find(n, m) where the def keyword is used to define a function and the find function is used to find the first occurrence of the specified value.
  2. 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 print function which followed by a comma and the declared variable q.
  3. 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 print function which followed by a comma and the declared variable r.
  4. The input values (n, m)are entered in the code within the find 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
  1. At the start, we declare the variables q, r with the function divmod along with the input values (14, 5) . We display the statements with the print function followed by the declared variables q  and r.
  2. 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 print function.
  3. 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.

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
  1. 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 variables n and d for the dividend and the divisor.
  2.  We use int() function before the input function which converts the specified value into an integer number.
  3. In the STDIN section of the code editor the input values are entered.
  4. We declare the operators // and % with the variables n and d which computes the quotient and the reminder of the input values. The values are saved in the variables Quotient and Reminder
  5. Then we display the output value using the print function along with the statement/string followed by the declared value, Quotient and Reminder.

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.

Leave a Reply

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...