【发布时间】:2014-03-19 09:10:19
【问题描述】:
C语言如何进行取模运算?
请阅读下面的代码。
#include <stdio.h>
int main(void)
{
int i = 100;
printf("%d\n", i%100);
printf("%d\n", i%240);
printf("%d\n", i%40);
printf("%d\n", i%10);
return 0;
}
结果是
$ ./modulus
0
100
20
0
请告诉我如何正确使用它。
编辑
对不起,我在代码中犯了一个错误。除法应该像下面的代码一样在另一边。
printf("%d\n", 100%i);
printf("%d\n", 240%i);
printf("%d\n", 40%i);
printf("%d\n", 10%i);
当值乘以 100 时,我期望得到 0;
我很抱歉这个错误。但是,伙计们,请撤消我的反对意见。
【问题讨论】:
-
你期待什么结果?
-
你用对了。
-
你想要什么输出。因为你的程序是正确的。
-
How to make modulus operation in C language?根据你已经使用的:) -
@MarounMaroun 不正确:"modulus and remainder are not the same"