Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Searching Algorithms
  • MCQs on Searching Algorithms
  • Tutorial on Searching Algorithms
  • Linear Search
  • Binary Search
  • Ternary Search
  • Jump Search
  • Sentinel Linear Search
  • Interpolation Search
  • Exponential Search
  • Fibonacci Search
  • Ubiquitous Binary Search
  • Linear Search Vs Binary Search
  • Interpolation Search Vs Binary Search
  • Binary Search Vs Ternary Search
  • Sentinel Linear Search Vs Linear Search
Open In App
Next Article:
Divide two integers without using multiplication, division and mod operator | Set2
Next article icon

Find Quotient and Remainder of two integer without using division operators

Last Updated : 25 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two positive integers dividend and divisor, our task is to find quotient and remainder. The use of division or mod operator is not allowed.

Examples:

Input : dividend = 10, divisor = 3 
Output : 3, 1 
Explanation: 
The quotient when 10 is divided by 3 is 3 and the remainder is 1.

Input : dividend = 11, divisor = 5 
Output : 2, 1 
Explanation: 
The quotient when 11 is divided by 5 is 2 and the remainder is 1. 
 

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: 
To solve the problem mentioned above we will use the Binary Search technique. We can implement the search method in range 1 to N where N is the dividend. Here we will use multiplication to decide the range. As soon as we break out of the while loop of binary search we get our quotient and the remainder can be found using the multiplication and subtraction operator. Handle the special case, when the dividend is less than or equal to the divisor, without the use of binary search.

Efficient Approach:

  1. Define a function find that takes four parameters - dividend, divisor, start, and end, and returns a pair of integers - quotient and remainder.
  2. Check if the start is greater than the end. If yes, return {0, dividend}, where 0 is the quotient and dividend is the remainder.
  3. Calculate the mid value as (start + end) / 2.
  4. Subtract the product of divisor and mid from the dividend and store it in a variable n.
  5. Check if n is greater than divisor. If yes, increment the mid by 1 and update the start to mid+1.
  6. Check if n is less than 0. If yes, decrement the mid by 1 and update the end to mid-1.
  7. If n is equal to the divisor, increment the mid by 1 and set n to 0.
  8. Return the pair {mid, n} as the quotient and remainder.
  9. Recursively call the find function with updated values of start and end until start becomes greater than end.
  10. Define a function divide that takes two parameters - dividend and divisor, and returns the quotient and remainder using the find function with start = 1 and end = dividend.
  11. In the main function, call the divide function with the given values of dividend and divisor, and store the returned pair in a variable ans.
  12. Print the quotient and remainder by accessing the first and second elements of ans respectively.

Below is the implementation of the above approach:  

C++
// C++ implementation to Find Quotient // and Remainder of two integer without // using / and % operator using Binary search  #include <bits/stdc++.h> using namespace std;  // Function to the quotient and remainder pair<int, int> find(int dividend, int divisor,                     int start, int end) {      // Check if start is greater than the end     if (start > end)         return { 0, dividend };      // Calculate mid     int mid = start + (end - start) / 2;      int n = dividend - divisor * mid;      // Check if n is greater than divisor     // then increment the mid by 1     if (n > divisor)         start = mid + 1;      // Check if n is less than 0     // then decrement the mid by 1     else if (n < 0)         end = mid - 1;      else {         // Check if n equals to divisor         if (n == divisor) {             ++mid;             n = 0;         }          // Return the final answer         return { mid, n };     }      // Recursive calls     return find(dividend, divisor, start, end); }  pair<int, int> divide(int dividend, int divisor) {     return find(dividend, divisor, 1, dividend); }  // Driver code int main(int argc, char* argv[]) {     int dividend = 10, divisor = 3;      pair<int, int> ans;      ans = divide(dividend, divisor);      cout << ans.first << ", ";     cout << ans.second << endl;      return 0; } 
Java
// JAVA implementation to Find Quotient // and Remainder of two integer without // using / and % operator using Binary search  class GFG{   // Function to the quotient and remainder static int[] find(int dividend, int divisor,                     int start, int end) {       // Check if start is greater than the end     if (start > end)         return new int[] { 0, dividend };       // Calculate mid     int mid = start + (end - start) / 2;       int n = dividend - divisor * mid;       // Check if n is greater than divisor     // then increment the mid by 1     if (n > divisor)         start = mid + 1;       // Check if n is less than 0     // then decrement the mid by 1     else if (n < 0)         end = mid - 1;       else {         // Check if n equals to divisor         if (n == divisor) {             ++mid;             n = 0;         }           // Return the final answer         return new int[] { mid, n };     }       // Recursive calls     return find(dividend, divisor, start, end); }   static int[]  divide(int dividend, int divisor) {     return find(dividend, divisor, 1, dividend); }   // Driver code public static void main(String[] args) {     int dividend = 10, divisor = 3;        int []ans = divide(dividend, divisor);       System.out.print(ans[0]+ ", ");     System.out.print(ans[1] +"\n");   } }  // This code contributed by sapnasingh4991 
Python3
# Python3 implementation to Find Quotient  # and Remainder of two integer without  # using / and % operator using Binary search   # Function to the quotient and remainder  def find(dividend, divisor,  start,  end) :      # Check if start is greater than the end      if (start > end) :         return ( 0, dividend );       # Calculate mid      mid = start + (end - start) // 2;       n = dividend - divisor * mid;       # Check if n is greater than divisor      # then increment the mid by 1      if (n > divisor) :         start = mid + 1;       # Check if n is less than 0      # then decrement the mid by 1      elif (n < 0) :         end = mid - 1;       else :         # Check if n equals to divisor          if (n == divisor) :              mid += 1;              n = 0;           # Return the final answer          return ( mid, n );           # Recursive calls      return find(dividend, divisor, start, end);   def divide(dividend, divisor) :       return find(dividend, divisor, 1, dividend);   # Driver code  if __name__ == "__main__" :      dividend = 10; divisor = 3;       ans = divide(dividend, divisor);       print(ans[0],", ",ans[1])  # This code is contributed by Yash_R 
C#
// C# implementation to Find Quotient // and Remainder of two integer without // using / and % operator using Binary search   using System;  public class GFG{    // Function to the quotient and remainder static int[] find(int dividend, int divisor,                     int start, int end) {        // Check if start is greater than the end     if (start > end)         return new int[] { 0, dividend };        // Calculate mid     int mid = start + (end - start) / 2;        int n = dividend - divisor * mid;        // Check if n is greater than divisor     // then increment the mid by 1     if (n > divisor)         start = mid + 1;        // Check if n is less than 0     // then decrement the mid by 1     else if (n < 0)         end = mid - 1;        else {         // Check if n equals to divisor         if (n == divisor) {             ++mid;             n = 0;         }            // Return the readonly answer         return new int[] { mid, n };     }        // Recursive calls     return find(dividend, divisor, start, end); }    static int[]  divide(int dividend, int divisor) {     return find(dividend, divisor, 1, dividend); }    // Driver code public static void Main(String[] args) {     int dividend = 10, divisor = 3;         int []ans = divide(dividend, divisor);        Console.Write(ans[0]+ ", ");     Console.Write(ans[1] +"\n");    } } // This code contributed by Princi Singh 
JavaScript
<script>  // Javascript implementation to Find Quotient // and Remainder of two integer without // using / and % operator using Binary search  // Function to the quotient and remainder function find(dividend, divisor, start, end) {          // Check if start is greater than the end     if (start > end)         return [0, dividend];      // Calculate mid     var mid = start + parseInt((end - start) / 2);      var n = dividend - divisor * mid;      // Check if n is greater than divisor     // then increment the mid by 1     if (n > divisor)         start = mid + 1;      // Check if n is less than 0     // then decrement the mid by 1     else if (n < 0)         end = mid - 1;      else      {                  // Check if n equals to divisor         if (n == divisor)          {             ++mid;             n = 0;         }          // Return the final answer         return [ mid, n];     }      // Recursive calls     return find(dividend, divisor, start, end); }  function divide(dividend, divisor) {     return find(dividend, divisor, 1, dividend); }  // Driver code var dividend = 10, divisor = 3; var ans = divide(dividend, divisor);  document.write(ans[0] + ", "); document.write(ans[1] + "\n");  // This code is contributed by gauravrajput1  </script> 

Output
3, 1 

Time Complexity: O(logN)
Space Complexity: O(n)

Approach: Brute force approach based on repeated subtraction

One approach to finding the quotient and remainder of two integers without using division or mod operators is by repeated subtraction. The basic idea is to subtract the divisor from the dividend until the dividend becomes less than the divisor. The number of times the subtraction is performed is the quotient, and the final value of the dividend is the remainder.

Here are the steps for this approach:

  1. Initialize the quotient to 0 and the remainder to the value of the dividend.
  2. While the remainder is greater than or equal to the divisor, subtract the divisor from the remainder and increment the quotient by 1.
  3. Return the quotient and remainder.
C++
#include <iostream> using namespace std;  void findQuotientAndRemainder(int dividend, int divisor, int& quotient, int& remainder) {     quotient = 0;     remainder = dividend;          while (remainder >= divisor) {         remainder -= divisor;         quotient += 1;     } }  int main() {     int dividend = 10;     int divisor = 3;     int quotient, remainder;          findQuotientAndRemainder(dividend, divisor, quotient, remainder);          cout << "Quotient: " << quotient << endl;     cout << "Remainder: " << remainder << endl;          return 0; } 
Java
// Java program to find quotient and remainder import java.util.*;  public class Main {      // Function to find the quotient and     // remainder     static void findQuotientAndRemainder(int dividend,                                          int divisor,                                          int[] qr)     {         qr[0] = 0;         qr[1] = dividend;         while (qr[1] >= divisor) {             qr[1] -= divisor;             qr[0] += 1;         }     }      // Driver Code     public static void main(String[] args)     {         int dividend = 10;         int divisor = 3;         int[] qr = new int[2];          findQuotientAndRemainder(dividend, divisor, qr);          System.out.println("Quotient: " + qr[0]);         System.out.println("Remainder: " + qr[1]);     } } 
Python3
def find_quotient_and_remainder(dividend, divisor):     quotient = 0     remainder = dividend          while remainder >= divisor:         remainder -= divisor         quotient += 1          return quotient, remainder dividend = 10 divisor = 3 quotient, remainder = find_quotient_and_remainder(dividend, divisor) print("Quotient:", quotient) print("Remainder:", remainder) 
C#
using System;  class Program {     static void FindQuotientAndRemainder(int dividend, int divisor, out int quotient, out int remainder) {         quotient = 0;         remainder = dividend;          while (remainder >= divisor) {             remainder -= divisor;             quotient += 1;         }     }      static void Main(string[] args) {         int dividend = 10;         int divisor = 3;         int quotient, remainder;          FindQuotientAndRemainder(dividend, divisor, out quotient, out remainder);          Console.WriteLine("Quotient: {0}", quotient);         Console.WriteLine("Remainder: {0}", remainder);          Console.ReadKey();     } } 
JavaScript
<script> // Javascript program to find quotient and remainder  // Function to find the quotient and remainder function findQuotientAndRemainder(dividend, divisor) {   let quotient = 0;   let remainder = dividend;    while (remainder >= divisor) {     remainder -= divisor;     quotient += 1;   }    return { quotient, remainder }; }  const dividend = 10; const divisor = 3;  const { quotient, remainder } = findQuotientAndRemainder(dividend, divisor);  document.write("Quotient: " + quotient + "<br>"); document.write("Remainder: " + remainder);  // This code is contributed by Susobhan Akhuli </script> 

Output
Quotient: 3 Remainder: 1 

Time Complexity: O(dividend/divisor)
Auxiliary Space: O(1)


Next Article
Divide two integers without using multiplication, division and mod operator | Set2
author
coder001
Improve
Article Tags :
  • Searching
  • Mathematical
  • DSA
Practice Tags :
  • Mathematical
  • Searching

Similar Reads

  • Divide two integers without using multiplication and division operator
    Given two integers a and b, the task is to find the quotient after dividing a by b without using multiplication, division, and mod operator. Note: If the quotient is strictly greater than 231 - 1, return 231 - 1 and if the quotient is strictly less than -231, then return -231. Examples: Input : a =
    10 min read
  • Divide two integers without using multiplication, division and mod operator | Set2
    Given two integers say a and b. Find the quotient after dividing a by b without using multiplication, division and mod operator.Examples: Input: a = 10, b = 3Output: 3 Input: a = 43, b = -8Output: -5 This problem has been already discussed here. In this post, a different approach is discussed.Approa
    7 min read
  • Program to find remainder without using modulo or % operator
    Given two numbers 'num' and 'divisor', find remainder when 'num' is divided by 'divisor'. The use of modulo or % operator is not allowed.Examples : Input: num = 100, divisor = 7 Output: 2 Input: num = 30, divisor = 9 Output: 3 Method 1 : C/C++ Code // C++ program to find remainder without using // m
    9 min read
  • Count divisors which generates same Quotient and Remainder
    Given a positive integer N, the task is to find the count of all the numbers M such that when the number N is divided by M, the quotient is equal to its remainder i.e (?N/M? = N mod M) where ? ? denotes the floor value of a given number. Examples: Input: N = 8Output: 2Explanation: When 8 is divided
    7 min read
  • Multiply two integers without using multiplication, division and bitwise operators, and no loops
    By making use of recursion, we can multiply two integers with the given constraints. To multiply x and y, recursively add x y times. Approach: Since we cannot use any of the given symbols, the only way left is to use recursion, with the fact that x is to be added to x y times. Base case: When the nu
    9 min read
  • C++ program to divide a number by 3 without using *, / , +, -, % operators
    For a given positive number we need to divide a number by 3 without using any of these *, /, +, – % operatorsExamples: Input : 48 Output : 16 Input : 16 Output : 5 Algorithm Take a number num, sum = 0while(num>3), shift the number left by 2 bits and sum = add(num >> 2, sum). Create a functi
    2 min read
  • Program for quotient and remainder of big number
    Given a string of numbers and given another number (say m) [0 <= m <= 10^18]. The task is to calculate the modulus of the given number.Examples: Input : num = "214" m = 5 Output : Remainder = 4 Quotient = 42 Input : num = "214755974562154868" m = 17 Output : Remainder = 15 Quotient = 126327043
    8 min read
  • Divide two number using Binary search without using any / and % operator
    Given two integers one is a dividend and the other is the divisor, we need to find the quotient when the dividend is divided by the divisor without the use of any " / " and " % " operators. Examples: Input: dividend = 10, divisor = 2Output: 5Explanation: 10/2 = 5. Input: dividend = 10, divisor = 3Ou
    7 min read
  • Quotient and remainder dividing by 2^k (a power of 2)
    Given a positive integer n as a dividend and another number m (a form of 2^k), find the quotient and remainder without performing actual division Examples: Input : n = 43, m = 8Output : Quotient = 5, Remainder = 3 Input : n = 58, m = 16Output : Quotient = 3, Remainder = 10 An approach using bitwise
    4 min read
  • Program to Division two integers of given base
    Given three positive integers Base B, Dividend, and Divisor, where Dividend and Divisor are Base-B integers, the task is to find dividend/divisor. Examples: Input: Dividend = 513, Divisor = 7, B = 8Output: 57Explanation: The value of 513/7 in base 8 is 57. Input: Dividend = 400, Divisor = 20 B = 8Ou
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences