# Program to find remainder without using modulo or % operator

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
```

Method 1 :

## C++

 `// C++ program to find remainder without using  ` `// modulo operator ` `#include ` `using` `namespace` `std; ` ` `  `// This function returns remainder of num/divisor   ` `// without using % (modulo) operator ` `int` `getRemainder(``int` `num, ``int` `divisor) ` `{ ` `   ``return` `(num - divisor * (num / divisor)); ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` ` ``//   cout << 100 %0; ` `    ``cout << getRemainder(100, 7); ` `    ``return` `0; ` `}  `

## Java

 `// Java program to find remainder without ` `// using modulo operator ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``// This function returns remainder of ` `    ``// num/divisor without using % (modulo) ` `    ``// operator ` `    ``static` `int` `getRemainder(``int` `num, ``int` `divisor) ` `    ``{ ` `        ``return` `(num - divisor * (num / divisor)); ` `    ``} ` `     `  `    ``// Driver program to test above functions ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `         `  `        ``// print 100 % 0; ` `        ``System.out.println(getRemainder(``100``, ``7``)); ` `    ``}  ` `} ` ` `  `// This code is contributed by Sam007. `

## Python

 `# Python program to find remainder  ` `# without using modulo operator ` ` `  `# This function returns remainder of  ` `# num/divisor without using % (modulo) ` `# operator ` `def` `getRemainder(num, divisor): ` `    ``return` `(num ``-` `divisor ``*` `(num ``/``/` `divisor)) ` ` `  ` `  `# Driver program to test above functions ` `num ``=` `100` `divisor ``=` `7` `print``(getRemainder(num, divisor)) ` ` `  `# This code is contributed by Danish Raza `

## C#

 `// C# program to find remainder without using  ` `// modulo operator ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``// This function returns remainder of ` `    ``// num/divisor without using %  ` `    ``// (modulo) operator ` `    ``static` `int` `getRemainder(``int` `num, ``int` `divisor) ` `    ``{ ` `        ``return` `(num - divisor * (num / divisor)); ` `    ``} ` `     `  `    ``// Driver program to test above functions ` `    ``public` `static` `void` `Main() ` `    ``{ ` `         `  `        ``// print 100 % 0; ` `        ``Console.Write(getRemainder(100, 7)); ` `    ``}  ` `} ` ` `  `// This code is contributed by Sam007. `

/div>

## PHP

 ` `

Output :

`2`

This method is contributed by Bishal Kumar Dubey

Method 2

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.

Following is the implementation of above idea. Thanks to eleventyone for suggesting this solution in a comment.

## C++

 `// C++ program to find remainder without using modulo operator ` `#include ` `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 "````; ` `        ``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; ` `}  `

## Java

 `// Java program to find remainder without ` `// using modulo operator ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``// This function returns remainder  ` `    ``// of num/divisor without using % ` `    ``// (modulo) operator ` `    ``static` `int` `getRemainder(``int` `num, ``int` `divisor) ` `    ``{ ` `         `  `        ``// Handle divisor equals to 0 case ` `        ``if` `(divisor == ``0``) ` `        ``{ ` `            ``System.out.println(``"Error: divisor "` `                          ``+ ````"can't be zero "````); ` `            ``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 ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `         `  `        ``// print 100 % 0; ` `        ``System.out.println(getRemainder(``100``, ``7``)); ` `    ``}  ` `} ` ` `  `// This code is contributed by Sam007. `

## Python

 `# Python program to find remainder without ` `# using modulo operator. This function  ` `# returns remainder of num/divisor without  ` `# using % (modulo) operator ` ` `  `def` `getRemainder(num, divisor): ` ` `  `    ``# Handle divisor equals to 0 case ` `    ``if` `(divisor ``=``=` `0``): ` `        ``return` `False` ` `  `    ``# 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' ` `    ``i ``=` `1` `    ``product ``=` `0` `    ``while` `(product <``=` `num): ` `            ``product ``=` `divisor ``*` `i ` `            ``i ``+``=` `1` `    ``# return remainder ` `    ``return` `num ``-` `(product ``-` `divisor) ` ` `  `# Driver program to test above functions ` `num ``=` `100` `divisor ``=` `7` `print``(getRemainder(num, divisor)) ` ` `  `# This code is contributed by Danish Raza `

## C#

 `// C# program to find remainder without ` `// using modulo operator ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// This function returns remainder ` `    ``// of num/divisor without using % ` `    ``// (modulo) operator ` `    ``static` `int` `getRemainder(``int` `num, ``int` `divisor) ` `    ``{ ` `         `  `        ``// Handle divisor equals to 0 case ` `        ``if` `(divisor == 0) ` `        ``{ ` `            ``Console.WriteLine(``"Error: divisor "` `                         ``+ ````"can't be zero "````); ` `            ``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 ` `    ``public` `static` `void` `Main() ` `    ``{ ` `         `  `        ``// print 100 %0; ` `        ``Console.Write(getRemainder(100, 7)); ` `    ``}  ` `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 ` `

Output :

`2`