#include <stdio.h> int main() { int dividend, divisor, quotient, remainder; printf("Enter dividend: "); scanf("%d", ÷nd); 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 Show 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", ÷nd); 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: 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 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:
Output: Quotient: 3 Remainder 1 Quotient: 19 Remainder 4Time 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 MethodIn 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 2Python find quotient and remainder using divmod() methodThe 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: 4Related Articles Program to calculate simple interest in Python
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 − ExampleLive 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)OutputEnter 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 : 44Explanation
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 Next Steps:
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. |