-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFind remainder without using modulo or % operator
55 lines (44 loc) · 1.37 KB
/
Find remainder without using modulo or % operator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
The idea is simple, we run a loop to find the largest multiple of ‘divisor’ that is smaller than or equal to ‘num’.
Once we find such a multiple, we subtract the multiple from ‘num’ to find the divisor.
// C++ program to find remainder without using modulo operator
#include <iostream>
using namespace std;
// This function returns remainder of num/divisor without
// using % (modulo) operator
int getRemainder(int num, int divisor)
{
// Handle divisor equals to 0 case
if (divisor == 0)
{
cout << "Error: divisor can't be zero \n";
return -1;
}
// Handle negative values
if (divisor < 0) divisor = -divisor;
if (num < 0) num = -num;
// Find the largest product of 'divisor' that is smaller
// than or equal to 'num'
int i = 1;
int product = 0;
while (product <= num)
{
product = divisor * i;
i++;
}
// return remainder
return num - (product - divisor);
}
// Driver program to test above functions
int main()
{
// cout << 100 %0;
cout << getRemainder(100, 7);
return 0;
}