用c编程找出1000之内的所有完数,并按照下面格式输出其因子:6 its factors are 1,2,3
时间: 2023-05-19 08:06:20 浏览: 127
以下是用 C 语言编写的找出 1000 以内的所有完数的代码:
```c
#include <stdio.h>
int main() {
int i, j, sum;
printf("Perfect numbers within 1000:\n");
for (i = 1; i <= 1000; i++) {
sum = 0;
for (j = 1; j < i; j++) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
printf("%d its factors are ", i);
for (j = 1; j < i; j++) {
if (i % j == 0) {
printf("%d,", j);
}
}
printf("\b are equal to %d\n", i);
}
}
return 0;
}
```
输出结果如下:
```
Perfect numbers within 1000:
6 its factors are 1,2,3 are equal to 6
28 its factors are 1,2,4,7,14 are equal to 28
496 its factors are 1,2,4,8,16,31,62,124,248 are equal to 496
```
其中,完数指一个数恰好等于它的因子之和,例如 6 = 1 + 2 + 3。
阅读全文