📜  无需使用模数或%运算符即可查找余数的程序

📅  最后修改于: 2021-05-04 12:40:12             🧑  作者: Mango

给定两个数字“ num”和“除数”,当“ num”除以“除数”时找到余数。不允许使用取模或%运算符。
例子 :

Input:  num = 100, divisor = 7
Output: 2

Input:  num = 30, divisor = 9
Output: 3

方法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.


PHP


Javascript


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


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


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 
");         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


C++
// C++ implementation of the approach
#include 
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


输出 :

2

此方法由Bishal Kumar Dubey提供

方法二

这个想法很简单,我们运行一个循环来查找小于或等于“ num”的“除数”的最大倍数。一旦找到这样的倍数,就从“ num”中减去倍数以找到除数。
以下是上述想法的实现。感谢eleventyone在评论中建议此解决方案。

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

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


Java脚本

// 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 
");         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

输出 :

2

方法3

继续从分子中减去分母,直到分子小于分母。

C++

// C++ implementation of the approach
#include 
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

Java脚本

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

输出 :

2