The Wayback Machine - https://web.archive.org/web/20240627105645/https://www.geeksforgeeks.org/program-to-find-remainder-without-using-modulo-or-operator/
Open In App

Program to find remainder without using modulo or % operator

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 <iostream>
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.


Python3




# 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.


PHP




<?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)
{
    $t = ($num - $divisor *
         (int)($num / $divisor));
    return $t;
}
 
// Driver Code
echo getRemainder(100, 7);
 
// This code is contributed by ajit
?>


Javascript




<script>
// Javascript program to find remainder
// without using modulo operator
 
// This function returns remainder
// of num/divisor without using
// % (modulo) operator
function getRemainder(num, divisor)
{
    let t = (num - divisor *
         parseInt(num / divisor));
    return t;
}
 
// Driver Code
document.write(getRemainder(100, 7));
 
// This code is contributed by _saurabh_jaiswal
</script>


Output : 

2

Time Complexity: O(1) 

Auxiliary Space: O(1)
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 <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;
}


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 \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.


Python3




# 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 \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
 
// 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 functions
echo getRemainder(100, 7);
 
// This code is contributed by ajit.
?>


Javascript




// 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 functions
document.write(getRemainder(100, 7));
 
// This code is contributed by _saurabh_jaiswal


Output : 

2

Time Complexity: O(n) 

Auxiliary Space: O(1)

 

Method 3

Keep subtracting the denominator from numerator until the numerator is less than the denominator. 
 

C++




// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function to return num % divisor
// without using % (modulo) operator
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 code
int main()
{
    int num = 100, divisor = 7;
    cout << getRemainder(num, divisor);
 
    return 0;
}


Java




// A Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return num % divisor
// without using % (modulo) operator
static 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 code
public static void main(String[] args)
{
    int num = 100, divisor = 7;
    System.out.println(getRemainder(num, divisor));
}
}
 
// This code is contributed by Princi Singh


Python3




# Python3 implementation of the approach
 
# Function to return num % divisor
# without using % (modulo) operator
def getRemainder(num, divisor):
 
    # While divisor is smaller
    # than n, keep subtracting
    # it from num
    while (num >= divisor):
        num -= divisor;
 
    return num;
 
# Driver code
if __name__ == '__main__':
 
    num = 100; divisor = 7;
    print(getRemainder(num, divisor));
 
# This code is contributed by Princi Singh


C#




// C# implementation of the approach
using System;
     
class GFG
{
 
// Function to return num % divisor
// without using % (modulo) operator
static 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 code
public static void Main(String[] args)
{
    int num = 100, divisor = 7;
    Console.WriteLine(getRemainder(num, divisor));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




// Javascript implementation of the approach
 
// Function to return num % divisor
// without using % (modulo) operator
function getRemainder(num, divisor)
{
 
    // While divisor is smaller
    // than n, keep subtracting
    // it from num
    while (num >= divisor)
        num -= divisor;
 
    return num;
}
 
// Driver code
let num = 100, divisor = 7;
document.write(getRemainder(num, divisor));
 
// This code is contributed by _saurabh_jaiswal


Output : 

2

Time Complexity: O(n) 

Auxiliary Space: O(1)

 



Previous Article
Next Article

Similar Reads

Maximize remainder of sum of a pair of array elements with different parity modulo K
Given an array arr[] of size N, consisting of N / 2 even and odd integers each, and an integer K, the task is to find the maximum remainder of sum of a pair of array elements of different parity modulo K. Examples: Input: arr[] = {3, 2, 4, 11, 6, 7}, K = 7Output: 6Explanation: Sum of a pair of array elements = 2 + 11Sum % K = 13 % 7 = 6.Therefore,
7 min read
Maximum frequency of a remainder modulo 2i
Given an octal number N, the task is to convert the number to decimal and then find the modulo with every power of 2 i.e. 2i such that i &gt; 0 and 2i &lt; N, and print the maximum frequency of the modulo.Examples: Input: N = 13 Output: 2 Octal(13) = decimal(11) 11 % 2 = 1 11 % 4 = 3 11 % 8 = 3 3 occurs the most i.e. 2 times. Input: N = 21 Output:
11 min read
Implementation of Chinese Remainder theorem (Inverse Modulo based implementation)
We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that: x % num[0] = rem[0], x % num[1] = rem[1], ....................... x % num[k-1] = rem[k-1] Example: Input: num[] = {3, 4, 5}, rem[] = {2, 3, 1} Output: 11 Explanation: 11 is the sm
11 min read
Find Quotient and Remainder of two integer without using division operators
Given two positive integers dividend and divisor, our task is to find quotient and remainder. The use of division or mod operator is not allowed. Examples: Input : dividend = 10, divisor = 3 Output : 3, 1 Explanation: The quotient when 10 is divided by 3 is 3 and the remainder is 1. Input : dividend = 11, divisor = 5 Output : 2, 1 Explanation: The
10 min read
Find largest element from array without using conditional operator
Given an array of n-elements, we have to find the largest element among them without using any conditional operator like greater than or less than.Examples: Input : arr[] = {5, 7, 2, 9} Output : Largest element = 9 Input : arr[] = {15, 0, 2, 15} Output : Largest element = 15 First Approach (Use of Hashing) : To find the largest element from the arr
8 min read
To find sum of two numbers without using any operator
Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used. Solution It's a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in printf() can be used to find the sum of two numbe
9 min read
Find XOR of two number without using XOR operator
Given two integers, find XOR of them without using the XOR operator, i.e., without using '^' in C/C++. Examples : Input: x = 1, y = 2Output: 3Input: x = 3, y = 5Output: 6Approach:A Simple Solution is to traverse all bits one by one. For every pair of bits, check if both are the same, set the corresponding bit like 0 in output, otherwise set it as 1
13 min read
Decimal to Binary using recursion and without using power operator
Given an integer N, the task is convert and print the binary equaiva;ent of N.Examples: Input: N = 13 Output: 1101Input: N = 15 Output: 1111 Approach Write a recursive function that takes an argument N and recursively calls itself with the value N / 2 as the new argument and prints N % 2 after the call. The base condition will be when N = 0, simply
3 min read
Divide two number using Binary search without using any / and % operator
Given two integers one is a dividend and the other is the divisor, we need to find the quotient when the dividend is divided by the divisor without the use of any " / " and " % " operators. Examples: Input: dividend = 10, divisor = 2Output: 5Explanation: 10/2 = 5. Input: dividend = 10, divisor = 3Output: 3Explanation: 10/3 = 3.33333... which is tru
7 min read
Program to find remainder when large number is divided by r
Given a number N, the task is to find the remainder when N is divided by R (a two-digit Number). The input of the Number may be very large. Examples: Input: N = 13589234356546756, R = 13 Output: 11 Input: N = 3435346456547566345436457867978, R = 17 Output: 13Get the digit of N one by one from left to right.For each digit, combine with next digit if
9 min read
three90RightbarBannerImg