C++ Program to Check Perfect Number

C++ Program to Check Perfect Number

Perfect numbers are positive integers that are equal to the sum of their proper divisors (excluding the number itself). For example, 6 is perfect because 1 + 2 + 3 = 6. Checking perfect numbers is not only a fun mathematical exercise but also a great way to practice loops, conditionals, and optimization in C++. In this article, we will explore several methods to check if a number is perfect in a beginner-friendly manner.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Basic Method to Check Perfect Number

This is the simplest approach to determine if a number is perfect. It loops through all numbers from 1 up to n-1 and sums up the divisors. If the sum equals the number, it is perfect.

#include <iostream>

using namespace std;

int main() {

    int n, sum = 0;

    cout << "Enter a number: " << endl;
    cin >> n;

    for(int i = 1; i < n; i++) {

        if(n % i == 0)
            sum += i;

    }

    if(sum == n)
        cout << n << " is a perfect number." << endl;

    else
        cout << n << " is not a perfect number." << endl;

    return 0;

}

In this program, we start with a sum of zero and iterate over all numbers less than n. For each divisor, we add it to the sum. By comparing the sum with the original number, we can determine perfection. Beginners can easily understand this method as it directly follows the definition of perfect numbers.

Program 2: Optimized Method Using Half of the Number

Checking all numbers up to n-1 is unnecessary because no divisor can be greater than n/2. We can optimize by iterating only up to n/2, which reduces computations.

#include <iostream>

using namespace std;

int main() {

    int n, sum = 0;

    cout << "Enter a number: " << endl;
    cin >> n;

    for(int i = 1; i <= n/2; i++) {

        if(n % i == 0)
            sum += i;

    }

    if(sum == n)
        cout << n << " is a perfect number." << endl;

    else
        cout << n << " is not a perfect number." << endl;

    return 0;

}

By limiting the loop to half the number, we avoid unnecessary checks while achieving the same result. This small optimization introduces beginners to efficient coding practices without complicating the logic.

Program 3: Using a Function

Encapsulating the perfect number check inside a function makes the code modular and reusable, which is useful when checking multiple numbers.

#include <iostream>

using namespace std;

bool isPerfect(int n) {

    int sum = 0;

    for(int i = 1; i <= n/2; i++) {

        if(n % i == 0)
            sum += i;

    }

    return sum == n;

}

int main() {

    int n;

    cout << "Enter a number: " << endl;
    cin >> n;

    if(isPerfect(n))
        cout << n << " is a perfect number." << endl;

    else
        cout << n << " is not a perfect number." << endl;

    return 0;

}

This program demonstrates the power of modular programming. The function isPerfect() can be reused for multiple numbers or integrated into larger programs. Beginners also get to practice function creation and calling.

Program 4: Using Known Perfect Numbers (Efficient for Large Numbers)

For very large numbers, calculating all divisors can be slow. Since perfect numbers are rare, we can use a list of known perfect numbers to quickly check if a number is perfect. This method is practical for applications where efficiency is crucial.

#include <iostream>
#include <vector>

using namespace std;

bool isPerfectKnown(int n) {

    // List of known even perfect numbers
    vector<int> knownPerfects {6, 28, 496, 8128, 33550336};

    for(int perfect : knownPerfects) {

        if(n == perfect)
            return true;

    }

    return false;

}

int main() {

    int n;

    cout << "Enter a number: " << endl;
    cin >> n;

    if(isPerfectKnown(n))
        cout << n << " is a perfect number." << endl;

    else
        cout << n << " is not a perfect number." << endl;

    return 0;

}

This approach is extremely efficient for checking large numbers because it avoids divisor computations entirely. Beginners can see how using precomputed data can optimize programs when computations are expensive.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about perfect numbers in C++:

Q1: What is a perfect number?
A perfect number is a positive integer equal to the sum of its proper divisors. For example, 6 and 28 are perfect numbers.

Q2: Are perfect numbers common?
No, perfect numbers are very rare, and only a few are known.

Q3: Can perfect numbers be odd?
All known perfect numbers are even. It is still an open question whether an odd perfect number exists.

Q4: Which method is best for large numbers?
Using a list of known perfect numbers is the most efficient method for very large numbers.

Q5: Why use functions for checking perfect numbers?
Functions make the code modular, reusable, and easier to read, which is good programming practice.

Conclusion

Checking perfect numbers in C++ helps beginners practice loops, conditionals, and modular coding. We explored four methods: the basic loop, an optimized loop up to n/2, using a reusable function, and checking against known perfect numbers for efficiency. By practicing these methods, beginners can strengthen their understanding of mathematical logic and programming techniques.

Additional & References

Understanding perfect numbers introduces important concepts in number theory and algorithm optimization. Beginners are encouraged to experiment with multiple numbers and combine these methods with other mathematical exercises.

Scroll to Top