一个数如果恰好等于它的因子之和,这个数就称为完数,C语言编程序找出1000之内的所有完数并按照6 its factors are 1,2,3的格式输出
时间: 2023-11-11 16:04:59 浏览: 82
以下是C语言编写的程序,实现了找出1000之内的所有完数并按照6 its factors are 1,2,3的格式输出的功能:
```c
#include <stdio.h>
int main() {
int i, j, sum;
printf("The perfect numbers between 1 and 1000 are:\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 1", i);
for (j = 2; j <= i/2; j++) {
if (i % j == 0) {
printf(", %d", j);
}
}
printf("\n");
}
}
return 0;
}
```
程序运行结果如下:
```
The perfect numbers between 1 and 1000 are:
6 its factors are 1, 2, 3
28 its factors are 1, 2, 4, 7, 14
496 its factors are 1, 2, 4, 8, 16, 31, 62, 124, 248
```
阅读全文