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

#include <stdio.h> int main() { int dividend, divisor, quotient, remainder; printf("Enter dividend: "); scanf("%d", &dividend); printf("Enter divisor: "); scanf("%d", &divisor); // Computes quotient quotient = dividend / divisor; // Computes remainder remainder = dividend % divisor; printf("Quotient = %d\n", quotient); printf("Remainder = %d", remainder); return 0; }

Output

Enter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1

In this program, the user is asked to enter two integers (dividend and divisor). They are stored in variables dividend and divisor respectively.

printf("Enter dividend: "); scanf("%d", &dividend); printf("Enter divisor: "); scanf("%d", &divisor);

Then the quotient is evaluated using / (the division operator), and stored in quotient.

quotient = dividend / divisor;

Similarly, the remainder is evaluated using % (the modulo operator) and stored in remainder.

remainder = dividend % divisor;

Finally, the quotient and remainder are displayed using printf().

printf("Quotient = %d\n", quotient); printf("Remainder = %d", remainder);

Learn more about how operators work in C programming.

Improve Article

Save Article

Like 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 4

Method 1: Naive approach

The naive approach is to find the quotient using the double division (//) operator and remainder using the modulus (%) operator.

Example:

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 4

Time 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:

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 4

Time Complexity: O(1)

Auxiliary Space: O(1)


In this post, 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.

Python find quotient and remainder using 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 Enter the first number: 89 Enter the second number: 3 Quotient: 29 Remainder 2

Python find quotient and remainder using divmod() method

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

Syntax of divmod() 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 Enter the first number: 34 Enter the second number: 6 Quotient: 5 Remainder: 4

Program to calculate simple interest in Python
Python program to input week number and print week day
Local variable referenced before assignment Python
Multiply all elements in list Python
Program to calculate simple interest in Python
Program to find area of triangle in Python
Python program to multiply two numbers
Python projects for beginners
Find average of n numbers in Python
Reverse pyramid pattern in Python
Stemming and Lemmatization in Python
Python program to convert decimal to binary
Print multiplication table in Python
Fibonacci Series In Python | Python Program To Print
File Handling in Python
How to convert XML to JSON in Python
Python XML to Dictionary
Python program to multiply two matrices

PythonServer Side ProgrammingProgramming

When it is required to read two numbers and print the quotient and remainder when they are divided, the ‘//’ and ‘%’ operators can be used.

Below is a demonstration of the same −

Example

 Live Demo

first_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) print("The first number is ") print(first_num) print("The second number is ") print(second_num) quotient_val = first_num//second_num remainder_val = first_num%second_num print("The quotient is :") print(quotient_val) print("The remainder is :") print(remainder_val)

Output

Enter the first number...44 Enter the second number...56 The first number is 44 The second number is 56 The quotient is : 0 The remainder is : 44

Explanation

  • The first and second numbers are taken as input from the user.

  • They are displayed on the console.

  • To find the quotient, the ‘//’ operator is used.

  • To find the remainder, the ‘%’ operator is used.

  • The operations output is assigned to two variables respectively.

  • This is displayed as output on the console.

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

Updated on 16-Apr-2021 12:05:07

This is a Python Program to read two numbers and print their quotient and remainder.

The program takes two numbers and prints the quotient and remainder.

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.

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)

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.

  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.

Check this: Python Books | Programming MCQs

  • 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

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

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.