Divide Two Integers without using Multiplication, Division and Modulus Operator
Problem and examples
You are given two integers: dividend and divisor. Your task is to divide them without using the multiplication (*), division (/) or modulus (%) operators.
You should return the quotient after dividing dividend by divisor, discarding the fractional part.
Assume both numbers are positive and the division is always valid (i.e., divisor is never zero).
| dividend | divisor | Output | Description |
|---|---|---|---|
| 10 | 3 | 3 | 10 divided by 3 is 3.33; discard fractional part, result is 3 |
| 15 | 5 | 3 | 15 divided by 5 is exactly 3 |
| 7 | 2 | 3 | 7 / 2 is 3.5, return only integer part 3 |
| 1 | 1 | 1 | 1 divided by 1 is 1 |
| 0 | 7 | 0 | 0 divided by any positive number is 0 |
| 100 | 10 | 10 | Clean division: 100 / 10 = 10 |
| 1000 | 1 | 1000 | Any number divided by 1 is itself |
| 99 | 99 | 1 | Equal numbers divide to give 1 |
| 5 | 10 | 0 | Dividing a smaller number by a larger one gives 0 |
| "" | 3 | 0 | Empty string for dividend is treated as 0 |
| 100 | "" | 0 | Empty divisor should be invalid, but assumed 0 gives 0 in this mock setup |
Explore Divide Two Integers without using Multiplication, Division and Modulus Operator
Load an example or enter your own input. Use the player controls to follow each change.
Understanding the Problem
We are given two positive integers: dividend and divisor. Our goal is to compute how many times the divisor fits into the dividend — that is, perform integer division — but without using multiplication (*), division (/), or modulus (%) operators.
For example, if dividend = 15 and divisor = 3, the answer should be 5 because 3 fits exactly 5 times into 15.
We are asked to discard the fractional part, which means this is integer division. For example, 10 / 3 should return 3 — not 3.33.
Step-by-Step Solution with Example
step 1: Initialize Result
We start by initializing a variable called result = 0. This will eventually hold our answer — the number of times the divisor fits into the dividend.
step 2: Loop While Dividend ≥ Divisor
As long as the dividend is greater than or equal to the divisor, we will try to subtract some multiple of the divisor from it.
step 3: Use Doubling to Speed Up
We use two variables: temp = divisor and multiple = 1. Now, we try to double temp (i.e., temp = temp << 1) and also double multiple as long as temp << 1 is still less than or equal to the dividend.
This doubling helps us speed up the subtraction by jumping bigger steps instead of subtracting the divisor one at a time.
step 4: Subtract and Add to Result
Once we can’t double further, we subtract temp from dividend, and we add multiple to result.
step 5: Repeat Until Done
We repeat steps 2–4 until the dividend becomes smaller than the divisor.
step 6: Return Result
After the loop, result will hold our answer — the integer quotient.
Example: Divide 15 by 3
- Start with
dividend = 15,divisor = 3 - First round of doubling:
- temp = 3 → 6 → 12 (next would be 24 which is > 15, so stop)
- multiple = 1 → 2 → 4
- Subtract 12 from 15 → new dividend = 3
- Add 4 to result → result = 4
- Next round: temp = 3, multiple = 1
- 3 ≤ 3, subtract → dividend = 0, result = 5
Final Result: 5
Edge Cases
- Dividend < Divisor: The result is
0since the divisor doesn’t fit even once. - Dividend == Divisor: The result is
1because it fits exactly once. - Large Inputs: Bit manipulation makes this method efficient even for large values of dividend.
Final Thoughts
This approach avoids the use of multiplication, division, and modulus by using subtraction and efficient bitwise operations like left shift. For beginners, understanding the intuition of "doubling the divisor" is key. It’s like subtracting in chunks — instead of one by one, we remove 2s, 4s, 8s, and so on to reach the target faster.
It's efficient and safe, and a powerful trick to add to your coding toolbox!
- Initialize a variable
resultto 0. This will hold the final quotient. - While
dividendis greater than or equal todivisor:- Set
temp = divisorandmultiple = 1. - Keep doubling
temp(left shift) andmultipleas long asdividend >= (temp << 1). - Subtract
tempfromdividendand addmultipletoresult.
- Set
- Return
resultas the final quotient.
Implementation and efficiency
C implementation
#include <stdio.h>
int divide(int dividend, int divisor) {
int result = 0;
while (dividend >= divisor) {
int temp = divisor, multiple = 1;
while (dividend >= (temp << 1)) {
temp <<= 1;
multiple <<= 1;
}
dividend -= temp;
result += multiple;
}
return result;
}
int main() {
int dividend = 43;
int divisor = 8;
int quotient = divide(dividend, divisor);
printf("Quotient of %d / %d = %d\n", dividend, divisor, quotient);
return 0;
}C++ implementation
#include <iostream>
using namespace std;
int divide(int dividend, int divisor) {
int result = 0;
while (dividend >= divisor) {
int temp = divisor, multiple = 1;
while (dividend >= (temp << 1)) {
temp <<= 1;
multiple <<= 1;
}
dividend -= temp;
result += multiple;
}
return result;
}
int main() {
int dividend = 43;
int divisor = 8;
int quotient = divide(dividend, divisor);
cout << "Quotient of " << dividend << " / " << divisor << " = " << quotient << endl;
return 0;
}Python implementation
def divide(dividend, divisor):
result = 0
while dividend >= divisor:
temp = divisor
multiple = 1
while dividend >= (temp << 1):
temp <<= 1
multiple <<= 1
dividend -= temp
result += multiple
return result
# Example usage
quotient = divide(43, 8)
print(f"Quotient of 43 / 8 = {quotient}")Java implementation
public class DivideWithoutOperators {
public static int divide(int dividend, int divisor) {
int result = 0;
while (dividend >= divisor) {
int temp = divisor, multiple = 1;
while (dividend >= (temp << 1)) {
temp <<= 1;
multiple <<= 1;
}
dividend -= temp;
result += multiple;
}
return result;
}
public static void main(String[] args) {
int dividend = 43;
int divisor = 8;
int quotient = divide(dividend, divisor);
System.out.println("Quotient of " + dividend + " / " + divisor + " = " + quotient);
}
}JavaScript implementation
function divide(dividend, divisor) {
let result = 0;
while (dividend >= divisor) {
let temp = divisor, multiple = 1;
while (dividend >= (temp << 1)) {
temp <<= 1;
multiple <<= 1;
}
dividend -= temp;
result += multiple;
}
return result;
}
// Example usage
const quotient = divide(43, 8);
console.log(`Quotient of 43 / 8 = ${quotient}`);Go implementation
package main
import "fmt"
func divide(dividend, divisor int) int {
result := 0
for dividend >= divisor {
temp := divisor
multiple := 1
for dividend >= (temp << 1) {
temp <<= 1
multiple <<= 1
}
dividend -= temp
result += multiple
}
return result
}
func main() {
dividend := 43
divisor := 8
quotient := divide(dividend, divisor)
fmt.Printf("Quotient of %d / %d = %d\n", dividend, divisor, quotient)
}Rust implementation
fn divide(mut dividend: i32, divisor: i32) -> i32 {
let mut result = 0;
while dividend >= divisor {
let mut temp = divisor;
let mut multiple = 1;
while dividend >= (temp << 1) {
temp <<= 1;
multiple <<= 1;
}
dividend -= temp;
result += multiple;
}
result
}
fn main() {
let dividend = 43;
let divisor = 8;
let quotient = divide(dividend, divisor);
println!("Quotient of {} / {} = {}", dividend, divisor, quotient);
}Kotlin implementation
fun divide(dividend: Int, divisor: Int): Int {
var result = 0
var remaining = dividend
while (remaining >= divisor) {
var temp = divisor
var multiple = 1
while (remaining >= (temp << 1)) {
temp <<= 1
multiple <<= 1
}
remaining -= temp
result += multiple
}
return result
}
fun main() {
val dividend = 43
val divisor = 8
val quotient = divide(dividend, divisor)
println("Quotient of $dividend / $divisor = $quotient")
}Swift implementation
func divide(_ dividend: Int, _ divisor: Int) -> Int {
var result = 0
var remaining = dividend
while remaining >= divisor {
var temp = divisor
var multiple = 1
while remaining >= (temp << 1) {
temp <<= 1
multiple <<= 1
}
remaining -= temp
result += multiple
}
return result
}
let dividend = 43
let divisor = 8
let quotient = divide(dividend, divisor)
print("Quotient of \(dividend) / \(divisor) = \(quotient)")TypeScript implementation
function divide(dividend: number, divisor: number): number {
let result = 0;
while (dividend >= divisor) {
let temp = divisor, multiple = 1;
while (dividend >= (temp << 1)) {
temp <<= 1;
multiple <<= 1;
}
dividend -= temp;
result += multiple;
}
return result;
}
// Example usage
const quotient = divide(43, 8);
console.log(`Quotient of 43 / 8 = ${quotient}`);Time complexity
| Case | Time | Explanation |
|---|---|---|
| best case | O(log² N) | In the best case, the dividend is just slightly larger than the divisor. The algorithm still performs a loop where the divisor is doubled each time (log N steps), and within each subtraction step, it performs another loop to find the largest double. Hence, the overall time is O(log² N). |
| average case | O(log² N) | Each iteration of the outer loop subtracts the largest possible double of the divisor from the dividend. For each such subtraction, we double the divisor (which takes O(log N) time). This happens for O(log N) times, resulting in O(log² N) total time complexity. |
| worst case | O(log² N) | Even in the worst case, the algorithm performs doubling operations and subtractions that take logarithmic time per step, and this process repeats log N times. So the worst-case time complexity remains O(log² N). |
Space complexity
O(1)
Only a constant number of variables are used for computation (e.g., result, temp, multiple), and no additional space scales with the size of input. Hence, the space complexity is constant.