C++ – How to find the perfect Number between 1 to 500 using for Loop

0 0
Read Time:1 Minute, 10 Second

A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself. In other words, the sum of all the positive divisors of a perfect number (excluding the number itself) is equal to the number itself.

For example, the number 6 is a perfect number because its proper divisors are 1, 2, and 3, and their sum is 1 + 2 + 3 = 6, which is equal to the number itself. Similarly, the number 28 is a perfect number because its proper divisors are 1, 2, 4, 7, and 14, and their sum is 1 + 2 + 4 + 7 + 14 = 28.

#include <iostream>
using namespace std;

bool isPerfectNumber(int number) {
    int sum = 0;
    for (int i = 1; i <= number / 2; i++) {
        if (number % i == 0) {
            sum += i;
        }
    }
    return sum == number;
}

int main() {
    cout << "Perfect numbers between 1 and 500:" <<endl;

    for (int i = 1; i <= 500; i++) {
        if (isPerfectNumber(i)) {
            cout << i << " ";
        }
    }

   

    return 0;
};

Perfect numbers have fascinated mathematicians for centuries, and they have been studied since ancient times. So far, all known perfect numbers are even, and they are relatively rare. The first few perfect numbers are 6, 28, 496, 8128, and so on. It is an open question in mathematics whether there are infinitely many perfect numbers or if there exist any odd perfect numbers.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment