Write a program to perform a division of two numbers without using the division operator (‘/’).

Approach #1: Division using Repeated Subtraction

We know that divisions can be solved by repeatedly subtracting the divisor from the dividend until it becomes less than the divisor. The total number of times the repeated subtraction is carried out is equal to the quotient.

This approach is demonstrated below in C, Java, and Python:

C


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Java


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Python


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Following is the recursive version of the above program in C, Java, and Python:

C


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Java


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Python


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Approach #2

C


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Java


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Python


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Approach #3: Division using Binary Operators

This approach is inspired by this answer in Stack Overflow.

C


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Java


Download  Run Code

Output:

The remainder is 1
The quotient is -3

Python


Download  Run Code

Output:

The remainder is 1
The quotient is -3