C语言 如何理解c中的模数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43302709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:38:38  来源:igfitidea点击:

How to make sense of modulo in c

cmodulo

提问by Cy Bu

i am not understanding this modulo in c languge.
For example:

我不理解 c 语言中的这个模数。
例如:

#include <stdio.h>
#include<math.h>

int main()
{
    int my_input[] = {23, 22, 21, 20, 19, 18};
    int n, mod;
    int nbr_items = sizeof(my_input) / sizeof(my_input[0]);

    for (n = 0; n < nbr_items; n++)
    {
        mod = my_input[n] % 4;
        printf("%d modulo %d --> %d\n", my_input[n], 4, mod);
    }
}

Gives:

给出:

23 modulo 4 --> 3
22 modulo 4 --> 2
21 modulo 4 --> 1
20 modulo 4 --> 0
19 modulo 4 --> 3
18 modulo 4 --> 2

I would have expected a number that i can make sense of.
Essentially i am trying to test if a number is divisible by 4.

我会期望一个我能理解的数字。
本质上,我试图测试一个数字是否可以被 4 整除。

回答by rakeshb

The modulo operator in C will give the remainder that is left over when one number is divided by another. For example, 23 % 4 will result in 3 since 23 is not evenly divisible by 4, and a remainder of 3 is left over.

C 中的模运算符将给出一个数除以另一个数时剩下的余数。例如,23 % 4 将导致 3,因为 23 不能被 4 整除,并且剩下 3 的余数。

If you want to output whether or not a number is divisible by 4, you need to output something other than just the mod result. Essentially, if mod = 0 than you know that one number is divisible by another.

如果你想输出一个数是否能被 4 整除,你需要输出一些不仅仅是 mod 结果的东西。本质上,如果 mod = 0,那么您就知道一个数可以被另一个数整除。

If you want to output whether or not the number is divisible by 4, I would suggest creating a new character that is set to "y" (yes) or "n" (no) depending on the result of the mod operation. Below is one possible implementation to generate a more meaningful output:

如果要输出数字是否可以被 4 整除,我建议创建一个新字符,根据 mod 操作的结果设置为“y”(是)或“n”(否)。以下是生成更有意义的输出的一种可能实现:

#include <stdio.h>
#include <ctype.h>
#include <math.h>

int main()
{
    int my_input[] = {23, 22, 21, 20, 19, 18};
    int n, mod;
    char is_divisible;
    int nbr_items = sizeof(my_input) / sizeof(my_input[0]);

    for (n = 0; n < nbr_items; n++)
    {
        mod = my_input[n] % 4;
        is_divisible = "y" ? mod == 0 : "n";
        printf("%d modulo %d --> %c\n", my_input[n], 4, is_divisible);
    }
}

This will give the following:

这将给出以下内容:

23 modulo 4 --> n
22 modulo 4 --> n
21 modulo 4 --> n
20 modulo 4 --> y
19 modulo 4 --> n
18 modulo 4 --> n

回答by paratrooper

I'm sure we know the basic division equation from high school math

我相信我们知道高中数学中的基本除法方程

dividend = divisor*quotient + remainder

股息=除数*商+余数

Now:
1. The "/" operator gives us the quotient.
2. The "%" operator gives us the remainder

现在:
1. "/" 运算符给我们商。
2. "%" 运算符给我们余数

example:

例子:

    say a = 23, b = 4
    a / b = 23 / 4 = 5
    a % b = 23 % 4 = 3

    23 = 4*5 + 3 

Here 4 is the quotient and 3 is the remainder.

这里4是商,3是余数。

If a number is perfectly divisible by a divisor, then remainder is zero.

如果一个数能被一个除数完全整除,则余数为零。

So:

所以:

    20/4 = 5 (quotient)
    20%4 = 0 (remainder)

To test if a no if divisible by 4, the check should be something like if (num % 4 == 0).
Hope this helps!

要测试是否可以被 4 整除,检查应该类似于if (num % 4 == 0)
希望这可以帮助!