1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-11-27 08:26:20 +00:00
202401-programming-assignments/【实践课内】11.函数2/6-1 使用函数输出指定范围内的完数.c

45 lines
680 B
C
Raw Permalink Normal View History

2024-11-22 08:22:46 +00:00
int factorsum(int number) {
int sum = 0;
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
return sum;
}
void print(int x) {
int cnt = 0;
printf("%d = ", x);
for (int i = 1; i < x; i++) {
if (x % i == 0) {
if (cnt++ != 0) {
printf(" + ");
}
printf("%d", i);
}
}
printf("\n");
}
void PrintPN(int m, int n) {
int cnt = 0;
for (int i = m; i <= n; i++) {
if (factorsum(i) == i) {
print(i);
cnt++;
}
}
if (cnt == 0) {
printf("No perfect number\n");
}
}