Python Program to Read Two Numbers and Print Their Quotient and Remainder

When dividing two numbers in Python, we often need both the quotient and remainder. Python provides the floor division operator (//) for quotient and the modulus operator (%) for remainder.

Understanding Division Operators

The floor division operator // returns the largest integer less than or equal to the division result, while the modulus operator % returns the remainder after division.

17 ÷ 5 = ? 17 // 5 = 3 (quotient) 17 % 5 = 2 (remainder) Verification: 3 × 5 + 2 = 17 ?

Example

Let's create a program that reads two numbers and calculates their quotient and remainder ?

first_num = 44
second_num = 5

print("The first number is:", first_num)
print("The second number is:", second_num)

quotient_val = first_num // second_num
remainder_val = first_num % second_num

print("The quotient is:", quotient_val)
print("The remainder is:", remainder_val)

# Verification
print("Verification:", quotient_val, "×", second_num, "+", remainder_val, "=", 
      quotient_val * second_num + remainder_val)
The first number is: 44
The second number is: 5
The quotient is: 8
The remainder is: 4
Verification: 8 × 5 + 4 = 44

Using divmod() Function

Python also provides the divmod() function that returns both quotient and remainder in a single operation ?

dividend = 17
divisor = 3

quotient, remainder = divmod(dividend, divisor)

print(f"{dividend} ÷ {divisor}")
print(f"Quotient: {quotient}")
print(f"Remainder: {remainder}")
17 ÷ 3
Quotient: 5
Remainder: 2

Handling Edge Cases

Always check for division by zero to prevent runtime errors ?

def calculate_quotient_remainder(dividend, divisor):
    if divisor == 0:
        return "Error: Division by zero is not allowed"
    
    quotient = dividend // divisor
    remainder = dividend % divisor
    
    return f"Quotient: {quotient}, Remainder: {remainder}"

# Test cases
print(calculate_quotient_remainder(15, 4))
print(calculate_quotient_remainder(10, 0))
print(calculate_quotient_remainder(-17, 5))
Quotient: 3, Remainder: 3
Error: Division by zero is not allowed
Quotient: -4, Remainder: 3

Conclusion

Use the // operator for integer division (quotient) and % for remainder. The divmod() function provides both values efficiently in one operation.

Updated on: 2026-03-25T19:06:10+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements