0
0
CppProgramBeginner · 2 min read

C++ Program to Check Perfect Number

A C++ program to check a perfect number sums all its divisors except itself and compares the sum with the number using code like for (int i = 1; i <= n / 2; i++) if (n % i == 0) sum += i; and then checks if (sum == n).
📋

Examples

Input6
Output6 is a perfect number.
Input28
Output28 is a perfect number.
Input10
Output10 is not a perfect number.
🧠

How to Think About It

To check if a number is perfect, find all its divisors except itself by checking numbers from 1 up to half of the number. Add these divisors together. If the total equals the original number, it is perfect; otherwise, it is not.
📐

Algorithm

1
Get the input number n.
2
Initialize sum to 0.
3
For each number i from 1 to n/2, check if i divides n evenly.
4
If yes, add i to sum.
5
After the loop, compare sum with n.
6
If sum equals n, print that n is perfect; else print it is not.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    cout << "Enter a number: ";
    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;
}
Output
Enter a number: 6 6 is a perfect number.
🔍

Dry Run

Let's trace the input 6 through the code to check if it is perfect.

1

Input number

n = 6, sum = 0

2

Check divisors from 1 to 3

i = 1, 6 % 1 == 0, sum = 0 + 1 = 1

3

Next divisor

i = 2, 6 % 2 == 0, sum = 1 + 2 = 3

4

Next divisor

i = 3, 6 % 3 == 0, sum = 3 + 3 = 6

5

Compare sum with n

sum = 6 equals n = 6, so 6 is perfect

in % i == 0?sum
1true1
2true3
3true6
💡

Why This Works

Step 1: Find divisors

We check all numbers from 1 to n/2 because no divisor (except n itself) can be greater than half of n.

Step 2: Sum divisors

Each divisor found is added to sum to get the total of all proper divisors.

Step 3: Compare sum with number

If the sum equals the original number, it means the number is perfect by definition.

🔄

Alternative Approaches

Using function to check perfect number
cpp
#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: ";
    cin >> n;
    if (isPerfect(n)) cout << n << " is a perfect number." << endl;
    else cout << n << " is not a perfect number." << endl;
    return 0;
}
This approach improves readability by separating logic into a function.
Optimized divisor check up to sqrt(n)
cpp
#include <iostream>
#include <cmath>
using namespace std;

bool isPerfect(int n) {
    if (n == 1) return false;
    int sum = 1;
    int root = sqrt(n);
    for (int i = 2; i <= root; i++) {
        if (n % i == 0) {
            sum += i;
            if (i != n / i) sum += n / i;
        }
    }
    return sum == n;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    if (isPerfect(n)) cout << n << " is a perfect number." << endl;
    else cout << n << " is not a perfect number." << endl;
    return 0;
}
This method reduces the number of checks by only going up to the square root, improving performance for large numbers.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops from 1 to n/2 to find divisors, so it runs in O(n) time.

Space Complexity

Only a few variables are used, so space complexity is O(1).

Which Approach is Fastest?

The optimized method checking divisors up to sqrt(n) is faster, running in O(√n) time, especially for large numbers.

ApproachTimeSpaceBest For
Simple loop to n/2O(n)O(1)Small to medium numbers
Function-based modular codeO(n)O(1)Readability and reuse
Optimized sqrt(n) checkO(√n)O(1)Large numbers and performance
💡
Only check divisors up to half of the number or better, up to its square root for efficiency.
⚠️
Beginners often include the number itself as a divisor, which makes the sum always larger than the number.