This blog will discuss the solutions to this problem on how to find quotient and remainder in C++. We will code these solutions in the C++ language. We will discuss different approaches to solve this problem.
To learn more about C++ programs, visit the C++ guided path.
Problem Statement
Write a program to find quotient and remainder of a dividend and divisor in C++.
Some Examples
Input
Dividend = 45
Divisor = 7
Output
Quotient = 6
Remainder = 3
Explanation
If 45 is divided by 7, 6 is the quotient, and 3 is the remainder. Here, 45 is the dividend, and 7 is the divisor.
Input
Dividend = 7
Divisor = 3
Output
Quotient = 2
Remainder = 1
Explanation
If 7 is divided by 3, 2 is the quotient, and 1 is the remainder.
Program to find quotient and remainder in C++
Now, we will solve this problem on how to find quotient and remainder in C++ using many different methods. These methods are as follows:
Using / and % operators
First, let's start with a straightforward program to find quotient and remainder in C++ using in-built division(/) and modulus(%) operators. A brief description of these operators is listed below in the table.
Operator
Description
Example
division(/)
Divides the numerator by the denominator
4/2 = 2
modulus(%)
Returns remainder after an integral division
5/3 = 2
Code:
#include <iostream>
using namespace std;
// Function to find quotient and remainder
void findQuotientAndRemainder(int a, int b)
{
// Find quotient and remainder
int quotient = a / b;
int remainder = a % b;
cout<<"Quotient = "<<quotient<<" and Remainder = "<<remainder<<endl;
return;
}
// Driver Code
int main()
{
// Given two number
int dividend = 45, divisor = 7;
// Function call
findQuotientAndRemainder(dividend , divisor );
findQuotientAndRemainder(7, 3);
return 0;
}
You can also try this code with Online C++ Compiler
#include <iostream>
using namespace std;
// Function to find quotient and remainder
void findQuotientAndRemainder(int a, int b)
{
// Find quotient and remainder
int quotient = a / b;
int remainder = a - (quotient*b);
cout<<"Quotient = "<<quotient<<" and Remainder = "<<remainder<<endl;
return;
}
// Driver Code
int main()
{
// Given two number
int dividend = 45, divisor = 7;
// Function call
findQuotientAndRemainder(dividend , divisor );
findQuotientAndRemainder(7, 3);
return 0;
}
You can also try this code with Online C++ Compiler
Which is the best method to find quotient and remainder in C++? The best method to find quotient and remainder in C++ is by using the / and % operator as the remainder formulae method may fail for larger numbers as (quotient*b) may exceed the supported range. Both methods have the same time complexity of O(1) and are very easy to implement.
What are the disadvantages of using the remainder formula method to find quotient and remainder in C++? The main disadvantage of using the remainder formula method is that this method may fail for larger numbers as (quotient*b) may exceed the supported range.
What is the disadvantage of using % for finding remainder? The modulus operator % computes the remainder when one integer is divided by another (modulus operator cannot be used for floating-type variables).
What is the remainder function in C++? The remainder() function in C++ can compute the floating-point remainder of numerator/denominator (rounded to nearest).
Key Takeaways
In this article, we have extensively discussed how to find quotient and remainder in C++ using different-different methods.