Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Some Examples
3.
Program to find quotient and remainder in C++
3.1.
Using / and % operators
3.2.
Using the Remainder formula
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Program to find quotient and remainder

Introduction

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
Run Code

 

The above code will find quotient and remainder using modulus operators. The output of the above code is displayed below:

Output

Time Complexity: O(1) because we find quotient and remainder using /and % operator in constant time.

Space Complexity: O(1) because only two extra variables are used.

Also readDecimal to Binary c++

Using the Remainder formula

Here, simply, we will use the remainder formula. The remainder formula will help us calculate the remainder obtained after dividing any two values. 

Remainder Formula: Dividend = Divisor × Quotient + Remainder.

We can now implement this approach.

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 - (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
Run Code

 

The above code will find quotient and remainder using the classic remainder formula. The output of the above code will be the same as method 1.

Output

Time Complexity: O(1) because we just did simple mathematical operations in constant time.

Space Complexity: O(1) because only two extra variables are used.

Try and compile with online c++ compiler.

Must Read Dynamic Binding in C++

FAQs

  1. 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.
     
  2. 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.
     
  3. 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).
     
  4. 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.

We hope that this blog has helped you enhance your knowledge on how to find quotient and remainder in C++, and if you would like to learn more, check out our articles on STL containers in C++Data Types in C++, and Implementing Sets Without C++ STL Containers. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass