试图弄清楚我的代码出了什么问题,请帮助我。该代码应该能够找出并打印出介于1到1000之间的理想数字以及数字的因数。我正在尝试调用perfect(number)函数,但始终弹出错误消息“需要Lvalue作为赋值的左操作数”。
#include <stdio.h>
int perfect(int number);
int main(void){
printf("The perfect numbers between 1 and 1000 are\n:");
for (int number = 1; number <=1000; number++){
perfect(number);
if (perfect(number) = 1){
printf("%d is a perfect number; ", number);
printf("It's factors are = ");
for (int i = 1; i <= number; i++){ //might need to change i<number
if (number % i == 0){
printf("%d", i);
}
}
}
}
return 0;
}
int perfect(int number){
int sum;
for(int number = 1;number <= 1000; number++){
sum = 0;
for (int i = 1; i <= number/2; i++){
if (number % i == 0){
sum = sum + i;
}
}
if (sum == number){
return 1;
}
else {
return 0;
}
}
}
答案 0 :(得分:2)
在C语言(以及一堆借用其语法的其他语言)中,=
是赋值运算符, not 是相等比较运算符。将if (perfect(number) = 1)
更改为if (perfect(number) == 1)
。