Q:

Write a program to read two numbers and print their quotient and remainder in Python

0

Write a program to read two numbers and print their quotient and remainder in Python

In this exercise, we will write a simple Python program that takes input from the user and calculates the quotient and remainder operation. There are different ways to achieve this task. Here, we are mentioning two ways to read two numbers and print their quotient and remainder in Python. The quotient is the result of the division, while the remainder is the value remaining after the division.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python Naive Method

In this method, first we take two inputs from the user using the input() function and store them in two variables, and then we obtain the quotient using division and the remainder using the modulus operator.

# Get quotient and remainder
# using Python program 
  
def find(n, m): 
      
    # for quotient 
    q = n//m 
    print("Quotient: ", q) 
      
    # for remainder 
    r = n%m 
    print("Remainder", r) 
      
# User Input 
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))

find(a,b)

Output of the above code-

Enter the first number: 20

Enter the second number: 5

Quotient:  4

Remainder 0

Python divmod() method

The divmod() function takes two numbers and returns a tuple containing the quotient and the remainder.

Syntax - 

divmod(divident, divisor)

Here, the divident is the number you want to divide, and the divisor is the number you want to divide with.

# Get quotient and remainder
# using Python program 
  
# Get user inputs
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))

q, r = divmod(a,b) 

print("Quotient: ", q) 
print("Remainder: ", r)

 Output of the above code-

Enter the first number: 43

Enter the second number: 2

Quotient:  21

Remainder:  1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to sort words in alphabetic... >>
<< How do you find the prime factor of a number in Py...