Divide two integers without using multiplication, division and mod operator

Given a two integers say a and b. Find the quotient after dividing a by b without using multiplication, division and mod operator.

Example:

Input : a = 10, b = 3
Output : 3

Input : a = 43, b = -8
Output :  -5


Approach : Keep subtracting the divisor from dividend until dividend becomes less than divisor. The dividend becomes the remainder, and the number of times subtraction is done becomes the quotient. Below is the implementation of above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to Divide two
// integers without using multiplication,
// division and mod operator
#include <bits/stdc++.h>
using namespace std;
  
// Function to divide a by b and
// return floor value it
int divide(int dividend, int divisor) {
  
  // Calculate sign of divisor i.e.,
  // sign will be negative only iff
  // either one of them is negative
  // otherwise it will be positive
  int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
  
  // Update both divisor and
  // dividend positive
  dividend = abs(dividend);
  divisor = abs(divisor);
  
  // Initialize the quotient
  int quotient = 0;
  while (dividend >= divisor) {
    dividend -= divisor;
    ++quotient;
  }
  
  return sign * quotient;
}
  
// Driver code
int main() {
  int a = 10, b = 3;
  cout << divide(a, b) << "\n";
  
  a = 43, b = -8;
  cout << divide(a, b);
  
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

/*Java implementation to Divide two
integers without using multiplication,
division and mod operator*/
  
import java.io.*;
  
class GFG 
{
      
    // Function to divide a by b and
    // return floor value it
    static int divide(int dividend, int divisor) 
    {
          
        // Calculate sign of divisor i.e.,
        // sign will be negative only iff
        // either one of them is negative
        // otherwise it will be positive
        int sign = ((dividend < 0) ^ 
                   (divisor < 0)) ? -1 : 1;
      
        // Update both divisor and
        // dividend positive
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
      
        // Initialize the quotient
        int quotient = 0;
          
        while (dividend >= divisor)
        {
            dividend -= divisor;
            ++quotient;
        }
      
        return sign * quotient;
    }    
      
    public static void main (String[] args) 
    {
        int a = 10;
        int b = 3;
          
        System.out.println(divide(a, b));
          
        a = 43;
        b = -8;
          
        System.out.println(divide(a, b));
    }
}
  
// This code is contributed by upendra singh bartwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation to Divide two
# integers without using multiplication,
# division and mod operator
  
# Function to divide a by b and
# return floor value it
def divide(dividend, divisor): 
  
    # Calculate sign of divisor i.e.,
    # sign will be negative only iff
    # either one of them is negative
    # otherwise it will be positive
    sign = -1 if ((dividend < 0) ^  (divisor < 0)) else 1
      
    # Update both divisor and
    # dividend positive
    dividend = abs(dividend)
    divisor = abs(divisor)
      
    # Initialize the quotient
    quotient = 0
    while (dividend >= divisor): 
        dividend -= divisor
        quotient += 1
      
      
    return sign * quotient
  
  
# Driver code
a = 10
b = 3
print(divide(a, b))
a = 43
b = -8
print(divide(a, b))
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to Divide two without 
// using multiplication, division and mod
// operator
using System;
  
class GFG {
      
    // Function to divide a by b and
    // return floor value it
    static int divide(int dividend, int divisor) 
    {
          
        // Calculate sign of divisor i.e.,
        // sign will be negative only iff
        // either one of them is negative
        // otherwise it will be positive
        int sign = ((dividend < 0) ^ 
                (divisor < 0)) ? -1 : 1;
      
        // Update both divisor and
        // dividend positive
        dividend = Math.Abs(dividend);
        divisor = Math.Abs(divisor);
      
        // Initialize the quotient
        int quotient = 0;
          
        while (dividend >= divisor)
        {
            dividend -= divisor;
            ++quotient;
        }
      
        return sign * quotient;
    
      
    public static void Main () 
    {
          
        int a = 10;
        int b = 3;
        Console.WriteLine(divide(a, b));
          
        a = 43;
        b = -8;
        Console.WriteLine(divide(a, b));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to Divide two
// integers without using multiplication,
// division and mod operator
  
// Function to divide a by b and
// return floor value it
function divide($dividend, $divisor) {
  
    // Calculate sign of divisor i.e.,
    // sign will be negative only iff
    // either one of them is negative
    // otherwise it will be positive
    $sign = (($dividend < 0) ^ 
             ($divisor < 0)) ? -1 : 1;
      
    // Update both divisor and
    // dividend positive
    $dividend = abs($dividend);
    $divisor = abs($divisor);
      
    // Initialize the quotient
    $quotient = 0;
    while ($dividend >= $divisor)
    {
        $dividend -= $divisor;
        ++$quotient;
    }
      
    return $sign * $quotient;
}
  
// Driver code
$a = 10;
$b = 3;
echo divide($a, $b)."\n";
  
$a = 43;
$b = -8;
echo divide($a, $b);
  
// This code is contributed by Sam007
?>

chevron_right



Output :

3
-5

Time complexity : O(a)
Auxiliary space : O(1)

Efficient Approach : Use bit manipulation in order to find the quotient. The divisor and dividend can be written as

dividend = quotient * divisor + remainder

As every number can be represented in base 2(0 or 1), represent the quotient in binary form by using shift operator as given below :

  1. Determine the most significant bit in the quotient. This can easily be calculated by iterating on the bit position i from 31 to 1.
  2. Find the first bit for which divisor << i is less than dividend and keep updating the ith bit position for which it is true.
  3. Add the result in temp variable for checking the next position such that (temp + (divisor << i) ) is less than dividend.
  4. Return the final answer of quotient after updating with corresponding sign.

Below is the implementation of above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to Divide two
// integers without using multiplication,
// division and mod operator
#include <bits/stdc++.h>
using namespace std;
  
// Function to divide a by b and
// return floor value it
int divide(long long dividend, long long divisor) {
  
  // Calculate sign of divisor i.e.,
  // sign will be negative only iff
  // either one of them is negative
  // otherwise it will be positive
  int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
  
  // remove sign of operands
  dividend = abs(dividend);
  divisor = abs(divisor);
  
  // Initialize the quotient
  long long quotient = 0, temp = 0;
  
  // test down from the highest bit and
  // accumulate the tentative value for
  // valid bit
  for (int i = 31; i >= 0; --i) {
  
    if (temp + (divisor << i) <= dividend) {
      temp += divisor << i;
      quotient |= 1LL << i;
    }
  }
  
  return sign * quotient;
}
  
// Driver code
int main() {
  int a = 10, b = 3;
  cout << divide(a, b) << "\n";
  
  a = 43, b = -8;
  cout << divide(a, b);
  
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to Divide 
// two integers without using 
// multiplication, division 
// and mod operator 
import java.io.*; 
import java.util.*; 
  
// Function to divide a by b 
// and return floor value it 
class GFG 
public static long divide(long dividend, 
                        long divisor) 
  
// Calculate sign of divisor 
// i.e., sign will be negative 
// only iff either one of them 
// is negative otherwise it 
// will be positive 
long sign = ((dividend < 0) ^ 
            (divisor < 0)) ? -1 : 1
  
// remove sign of operands 
dividend = Math.abs(dividend); 
divisor = Math.abs(divisor); 
  
// Initialize the quotient 
long quotient = 0, temp = 0
  
// test down from the highest 
// bit and accumulate the 
// tentative value for 
// valid bit 
// 1<<31 behaves incorrectly and gives Integer
// Min Value which should not be the case, instead 
  // 1L<<31 works correctly. 
for (int i = 31; i >= 0; --i) 
  
    if (temp + (divisor << i) <= dividend) 
    
        temp += divisor << i; 
        quotient |= 1L << i; 
    
  
return (sign * quotient); 
  
// Driver code 
public static void main(String args[]) 
int a = 10, b = 3
System.out.println(divide(a, b)); 
  
int a1 = 43, b1 = -8
System.out.println(divide(a1, b1)); 
  
  
  
// This code is contributed 
// by Akanksha Rai(Abby_akku) 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to 
# Divide two integers 
# without using multiplication,
# division and mod operator
  
# Function to divide a by 
# b and return floor value it
def divide(dividend, divisor):
      
    # Calculate sign of divisor 
    # i.e., sign will be negative
    # either one of them is negative
    # only iff otherwise it will be
    # positive
      
    sign = (-1 if((dividend < 0) ^ 
                  (divisor < 0)) else 1);
      
    # remove sign of operands
    dividend = abs(dividend);
    divisor = abs(divisor);
      
    # Initialize
    # the quotient
    quotient = 0;
    temp = 0;
      
    # test down from the highest 
    # bit and accumulate the 
    # tentative value for valid bit
    for i in range(31, -1, -1):
        if (temp + (divisor << i) <= dividend):
            temp += divisor << i;
            quotient |= 1 << i;
      
    return sign * quotient;
  
# Driver code
a = 10;
b = 3;
print(divide(a, b));
  
a = 43;
b = -8;
print(divide(a, b));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to Divide
// two integers without using 
// multiplication, division 
// and mod operator
using System;
  
// Function to divide a by b
// and return floor value it
class GFG
{
public static long divide(long dividend, 
                          long divisor) 
{
  
// Calculate sign of divisor 
// i.e., sign will be negative 
// only iff either one of them 
// is negative otherwise it 
// will be positive
long sign = ((dividend < 0) ^ 
             (divisor < 0)) ? -1 : 1;
  
// remove sign of operands
dividend = Math.Abs(dividend);
divisor = Math.Abs(divisor);
  
// Initialize the quotient
long quotient = 0, temp = 0;
  
// test down from the highest 
// bit and accumulate the 
// tentative value for
// valid bit
for (int i = 31; i >= 0; --i) 
{
  
    if (temp + (divisor << i) <= dividend) 
    {
        temp += divisor << i;
        quotient |= 1LL << i;
    }
}
  
return (sign * quotient);
}
  
// Driver code
public static void Main() 
{
int a = 10, b = 3;
Console.WriteLine(divide(a, b));
  
int a1 = 43, b1 = -8;
Console.WriteLine(divide(a1, b1));
  
}
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to 
// Divide two integers 
// without using multiplication,
// division and mod operator
  
// Function to divide a by 
// b and return floor value it
function divide($dividend
                $divisor
{
  
// Calculate sign of divisor 
// i.e., sign will be negative
// either one of them is negative
// only iff otherwise it will be
// positive
$sign = (($dividend < 0) ^ 
         ($divisor < 0)) ? -1 : 1;
  
// remove sign of operands
$dividend = abs($dividend);
$divisor = abs($divisor);
  
// Initialize
// the quotient
$quotient = 0;
$temp = 0;
  
// test down from the highest 
// bit and accumulate the 
// tentative value for valid bit
for ($i = 31; $i >= 0; --$i
{
  
    if ($temp + ($divisor << $i) <= $dividend
    {
        $temp += $divisor << $i;
        $quotient |= (double)(1) << $i;
    }
}
  
return $sign * $quotient;
}
  
// Driver code
$a = 10;
$b = 3;
echo divide($a, $b). "\n";
  
$a = 43;
$b = -8;
echo divide($a, $b);
  
// This code is contributed by mits
?>

chevron_right



Output :

3
-5

Time complexity : O(log(a))
Auxiliary space : O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.