题目:编写一个程序,显示求模运算的结果。把用户输入的1个整数作为求模运算符的第2个运算对象,该数在运算过程中保持不变。
用户后面输入的数是第1个运算对象。当用户输入一个非正值时,程序结束。
#include
int main(void)
{
int Fixed_value, Random_value;//固定值, 随机值//
int value;
printf(" This program computes moduli.\n");
printf("Enter an integer to serve as the second operand:" );
scanf_s("%d", &Fixed_value);
printf("Now enter the first operand:");
scanf_s("%d", &Random_value);
printf("%d %% %d is %d\n", Random_value, Fixed_value,
value, value = Random_value % Fixed_value);
while (Random_value > 0)
{
printf("Enter next number for first operand(<=0 to quit):");
scanf_s("%d", &Random_value);
printf("%d %% %d is %d\n", Random_value, Fixed_value, value,
value = Random_value % Fixed_value);
}
printf("Done\n");
return 0;
}
我的代码输入结果:
This program computes moduli.
Enter an integer to serve as the second operand:256
Now enter the first operand:438
438 % 256 is 182
Enter next number for first operand(<=0 to quit):1234567
1234567 % 256 is 135
Enter next number for first operand(<=0 to quit):0
0 % 256 is 0
Done
正确答案是:
This program computes moduli.
Enter an integer to serve as the second operand:256
Now enter the first operand:438
438 % 256 is 182
Enter next number for first operand(<=0 to quit):1234567
1234567 % 256 is 135
Enter next number for first operand(<=0 to quit):0
Done
输入:0
是没有 0 % 256 is 0
这样写:
#include <stdio.h>
int main(void)
{
int Fixed_value, Random_value;//固定值, 随机值//
int value;
printf(" This program computes moduli.\n");
printf("Enter an integer to serve as the second operand:" );
scanf_s("%d", &Fixed_value);
printf("Now enter the first operand:");
scanf_s("%d", &Random_value);
printf("%d %% %d is %d\n", Random_value, Fixed_value,
value, value = Random_value % Fixed_value);
while (1)
{
printf("Enter next number for first operand(<=0 to quit):");
scanf_s("%d", &Random_value);
if (Random_value <= 0) break;
printf("%d %% %d is %d\n", Random_value, Fixed_value, value,
value = Random_value % Fixed_value);
}
printf("Done\n");
return 0;
}
把while中第二个printf语句放到while循环第一行,while上面的printf语句删掉