View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m.
Examples:
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4Method 1: Naive approach
The naive approach is to find the quotient using the double division (//) operator and remainder using the modulus (%) operator.
Example:
Python3
def find(n, m):
q = n//m
print("Quotient: ", q)
r = n%m
print("Remainder", r)
find(10, 3)
find(99, 5)
Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2: Using divmod() method
Divmod() method takes two numbers as parameters and returns the tuple containing both quotient and remainder.
Example:
Python3
q, r = divmod(10, 3)
print("Quotient: ", q)
print("Remainder: ", r)
q, r = divmod(99, 5)
print("Quotient: ", q)
print("Remainder: ", r)
Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4Time Complexity: O(1)
Auxiliary Space: O(1)
In Division The number which we divide is called the dividend. The number by which we divide is called the divisor. The result obtained is called the quotient. The number left over is called the remainder.
Problem Definition
Create a Python program to compute Quotient and reminder of two given numbers.
Program
divisor = 5 dividend = 27 quotient = dividend//divisor reminder = dividend % divisor print("Quotient is", quotient) print("Reminder is",reminder)Output
Quotient is 5 Reminder is 2First, the numbers are saved in respective variables then to compute quotient we used the floor division // operator that returns the integer value of the quotient and for the reminder, the modulus % operator is used. Later we are just printing out the variables.
Problem Definition
Create a Python program to compute Quotient and reminder of two user-provided numbers in real-time.
Program
divisor = int(input("Enter the divisor")) dividend = int(input("Enter the dividend")) quotient = dividend//divisor reminder = dividend % divisor print("Quotient is {} and Reminder is {}".format(quotient, reminder))Output
Enter the divisor 5 Enter the dividend 27 Quotient is 5 and Reminder is 2Here we are taking input from the user using the Python's built-in input() method then we are converting it to an integer using the int() method because input() returns the objects as a string object.
Later we are performing the arithmetic operation and printing out the results using string formatting.
Recommended Read
- How To Convert Data Types in Python
- How To Use String Formatting In Python
PROGRAMS
This is a Python Program to read two numbers and print their quotient and remainder. Problem Description
The program takes two numbers and prints the quotient and remainder.
Problem Solution
1. Take in the first and second number and store it in separate variables.
2. Then obtain the quotient using division and the remainder using
modulus operator.
3. Exit.
Program/Source Code
Here is the source code of the Python Program to read two numbers and print their quotient and remainder. The program output is also shown below.
a=int(input("Enter the first number: ")) b=int(input("Enter the second number: ")) quotient=a//b remainder=a%b print("Quotient is:",quotient) print("Remainder is:",remainder)
Program Explanation
1. User must enter the first and second number .
2. The quotient is obtained using true division (// operator).
3. The modulus operator gives the remainder when a is divided by b.
Runtime Test Cases
Case 1: Enter the first number: 15 Enter the second number: 7 Quotient is: 2 Remainder is: 1 Case 2: Enter the first number: 125 Enter the second number: 7 Quotient is: 17 Remainder is: 6
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.