Program to find remainder without using modulo or % operator - GeeksforGeeks

14 Jan, 2022
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++
Java
Python3
C#
PHP
Javascript
// C++ program to find remainder without using// modulo operator#include <iostream>using namespace std; // This function returns remainder of num/divisor// without using % (modulo) operatorint getRemainder(int num, int divisor){    return (num - divisor * (num / divisor));} // Driver program to test above functionsint main(){    // cout << 100 %0;    cout << getRemainder(100, 7);    return 0;}
// Java program to find remainder without// using modulo operatorimport 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 program to find remainder# without using modulo operator # This function returns remainder of# num / divisor without using % (modulo)# operatordef getRemainder(num, divisor):    return (num - divisor * (num // divisor))  # Driver program to test above functionsnum = 100divisor = 7print(getRemainder(num, divisor)) # This code is contributed by Danish Raza
// C# program to find remainder without using// modulo operatorusing 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.
<?php// PHP program to find remainder// without using modulo operator // This function returns remainder// of num/divisor without using// % (modulo) operatorfunction getRemainder($num, $divisor){    $t = ($num - $divisor *         (int)($num / $divisor));    return $t;} // Driver Codeecho getRemainder(100, 7); // This code is contributed by ajit?>
<script>// Javascript program to find remainder// without using modulo operator // This function returns remainder// of num/divisor without using// % (modulo) operatorfunction getRemainder(num, divisor){    let t = (num - divisor *         parseInt(num / divisor));    return t;} // Driver Codedocument.write(getRemainder(100, 7)); // This code is contributed by _saurabh_jaiswal</script>
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++
Java
Python3
C#
PHP
Javascript
// C++ program to find remainder without using modulo operator#include <iostream>using namespace std; // This function returns remainder of num/divisor without// using % (modulo) operatorint 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 functionsint main(){    // cout << 100 %0;    cout << getRemainder(100, 7);    return 0;}
// Java program to find remainder without// using modulo operatorimport 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 \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    public static void main(String[] args)    {         // print 100 % 0;        System.out.println(getRemainder(100, 7));    }} // This code is contributed by Sam007.
# 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 functionsnum = 100divisor = 7print(getRemainder(num, divisor)) # This code is contributed by Danish Raza
// C# program to find remainder without// using modulo operatorusing 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 \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    public static void Main()    {         // print 100 %0;        Console.Write(getRemainder(100, 7));    }} // This code is contributed by Sam007.
<?php // php program to find remainder without// using modulo operator // This function returns remainder of// num/divisor without using % (modulo)// operator function getRemainder($num, $divisor){         // Handle divisor equals to 0 case    if ($divisor == 0)    {        echo "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'    $i = 1;    $product = 0;    while ($product <= $num)    {        $product = $divisor * $i;        $i++;    }     // return remainder    return $num - ($product - $divisor);} // Driver program to test above functionsecho getRemainder(100, 7); // This code is contributed by ajit.?>
// Javascript program to find remainder without// using modulo operator // This function returns remainder of// num/divisor without using % (modulo)// operator function getRemainder(num, divisor){         // Handle divisor equals to 0 case    if (divisor == 0)    {        document.write("Error: divisor can't be zero <br>");        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'    let i = 1;    let product = 0;    while (product <= num)    {        product = divisor * i;        i++;    }     // return remainder    return num - (product - divisor);} // Driver program to test above functionsdocument.write(getRemainder(100, 7)); // This code is contributed by _saurabh_jaiswal
Output : 
2
This article is contributed by Chirag. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Method 3
Keep subtracting the denominator from numerator until the numerator is less than the denominator.  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function to return num % divisor// without using % (modulo) operatorint getRemainder(int num, int divisor){     // While divisor is smaller    // than n, keep subtracting    // it from num    while (num >= divisor)        num -= divisor;     return num;} // Driver codeint main(){    int num = 100, divisor = 7;    cout << getRemainder(num, divisor);     return 0;}
// A Java implementation of the approachimport java.util.*; class GFG{ // Function to return num % divisor// without using % (modulo) operatorstatic int getRemainder(int num, int divisor){     // While divisor is smaller    // than n, keep subtracting    // it from num    while (num >= divisor)        num -= divisor;     return num;} // Driver codepublic static void main(String[] args){    int num = 100, divisor = 7;    System.out.println(getRemainder(num, divisor));}} // This code is contributed by Princi Singh
# Python3 implementation of the approach # Function to return num % divisor# without using % (modulo) operatordef getRemainder(num, divisor):     # While divisor is smaller    # than n, keep subtracting    # it from num    while (num >= divisor):        num -= divisor;     return num; # Driver codeif __name__ == '__main__':     num = 100; divisor = 7;    print(getRemainder(num, divisor)); # This code is contributed by Princi Singh
// C# implementation of the approachusing System;     class GFG{ // Function to return num % divisor// without using % (modulo) operatorstatic int getRemainder(int num, int divisor){     // While divisor is smaller    // than n, keep subtracting    // it from num    while (num >= divisor)        num -= divisor;     return num;} // Driver codepublic static void Main(String[] args){    int num = 100, divisor = 7;    Console.WriteLine(getRemainder(num, divisor));}} // This code is contributed by PrinciRaj1992
// Javascript implementation of the approach // Function to return num % divisor// without using % (modulo) operatorfunction getRemainder(num, divisor){     // While divisor is smaller    // than n, keep subtracting    // it from num    while (num >= divisor)        num -= divisor;     return num;} // Driver codelet num = 100, divisor = 7;document.write(getRemainder(num, divisor)); // This code is contributed by _saurabh_jaiswal
Output : 
2
 
Sam007
jit_t
Swathi__Hegde
princi singh
princiraj1992
_saurabh_jaiswal
amartyaghoshgfg
Chalk Studio
Modular Arithmetic
Mathematical
Mathematical
Modular Arithmetic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Sieve of Eratosthenes
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array