C加加编程找出n以内的所有完数
时间: 2024-06-13 11:06:58 浏览: 109
查找1到n的完数(c语言版)
4星 · 用户满意度95%
以下是C++编程找出n以内的所有完数的代码:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
cout << i << " its factors are ";
for (int j = 1; j < i; j++) {
if (i % j == 0) {
cout << j << " ";
}
}
cout << endl;
}
}
return 0;
}
```
阅读全文