用C语言,一个数如果恰好等于它的因子之和,这个数就称为完数。求1000之内的所有完数
时间: 2023-11-25 20:05:39 浏览: 204
基于visual Studio2013解决C语言竞赛题之完数
以下是C语言代码,用于找出1000之内的所有完数,并按照指定格式输出其因子:
```c
#include <stdio.h>
int main() {
int i, j, sum;
printf("完数有:\n");
for (i = 2; i < 1000; i++) {
sum = 0;
for (j = 1; j < i; j++) {
if ((i % j) == 0) {
sum = j + sum;
}
}
if (i == sum) {
printf("%d its factors are ", i);
for (j = 1; j < i; j++) {
if ((i % j) == 0) {
printf("%d,", j);
}
}
printf("\b.\n");
}
}
return 0;
}
```
输出结果为:
```
完数有:
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.
```
阅读全文