Write a program in C++ to find the perfect numbers between 1 and 10000.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; int main() { cout << "\n\n Find the perfect numbers between 1 and 10000:\n"; cout << "------------------------------------------------\n"; int i = 1, u = 1, sum = 0; cout << "\n The perfect numbers between 1 to 500 are: \n"; while (i <= 10000) { while (u <= 10000) { if (u < i) { if (i % u == 0) sum = sum + u; } u++; } if (sum == i) { cout << i << " " << "\n"; } i++; u = 1; sum = 0; } } |
Output: