Be the first user to complete this post

  • 0
Add to List
Beginner

226. Find remainder without using modulo operator

Objec­tive:  Write Given two integers ‘number’ and ‘divisor’, Write an algorithm to find the remainder if ‘number’ is divided by ‘divisor’.

Condition: You are not allowed to use modulo or % operator.

Example:

num = 10, divisor = 4
remainder = 2

num = 11, divisor = 2
remainder = 1

This is a fun puzzle that is asked in the interview.

Approach:

1.     This problem will become very trivial if the use of the modulo or % operator is allowed.

2.     Idea is to Keep subtracting the divisor from the number till the number>=divisor.

3.     Once the step above is done, the remaining value of the number will be the remainder.

Example:

number = 10, divisor = 4
number = number – divisor => 10 – 4 = 6
number = number – divisor => 6 – 4 = 2
remainder = 2

Output:

Number: 10, divisor: 4. remainder: 2