Perfect Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C - Perfect Number check (short & working) */
#include <stdio.h>
int main() {
    int n, sum = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n/2; i++)
        if (n % i == 0) sum += i;
    printf(sum == n ? "Perfect\n" : "Not Perfect\n");
    return 0;
}

C Output

Input:
28

Output:
Perfect



C++ Program

// C++ - Perfect Number check (short & working)
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, sum = 0; 
    cin >> n;
    for (int i = 1; i <= n/2; i++)
        if (n % i == 0) sum += i;
    cout << (sum == n ? "Perfect" : "Not Perfect") << '\n';
}

C++ Output

Input:
6

Output:
Perfect



JAVA Program

// Java - Perfect Number check (short & working)
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt(), sum = 0;
    for (int i = 1; i <= n/2; i++)
        if (n % i == 0) sum += i;
    System.out.println(sum == n ? "Perfect" : "Not Perfect");
  }
}

JAVA Output

Input:
12

Output:
Not Perfect



Python Program

# Python - Perfect Number check (short & working)
n = int(input())
print("Perfect" if sum(i for i in range(1, n//2+1) if n % i == 0) == n else "Not Perfect")

Python Output

Input:
496

Output:
Perfect



In-Depth Learning – Entire Concept in Paragraphs
Example
For input 28 in C, the loop iterates from 1 to 14, summing up all divisors: 1, 2, 4, 7, and 14. Their total is 28, which is just as much as the original number. That makes 28 a perfect number — that is, it equals the sum of its proper divisors (excluding it). If the total is less, it's a deficient number; if greater, it's abundant.

Real-Life Analogy
Imagine a perfect number being a "friend who returns exactly what they are given." If an individual is given assistance from precisely the same number of people they assist, without any surplus or dearth, that equilibrium is similar to that of a perfect number's attribute. It's mathematical harmony — the parts (divisors) equal the whole perfectly.

Learning Insights
This exercise illustrates divisor iteration, modulus operation, and conditional testing — all programming essentials. It's also a nice example of teaching time complexity thinking: here we iterate up to n/2, so this is O(n) worst-case. Optimistic students can iterate only up to sqrt(n) and add both divisors when they are found. Students also understand why you don't add n itself to the sum and thus get false positives.

Why It Matters
Mathematically, perfect numbers are uncommon and interesting. The smallest of them are 6, 28, 496, and 8128 — and all known even perfect numbers have a direct relationship with Mersenne primes via Euclid's formula. Although not ordinary in real-world software, they enhance logical reasoning and divisor-management skills. Awareness of them also gets you ready for interview questions involving lots of math where the skill of handling factors in a timely manner comes into play. 

Interview Use Case
In interviews, perfect number checking is usually camouflaged in number property, divisor sum, or classification problems (perfect, abundant, deficient). The interviewer might want you to make large inputs efficient, optimize the checking of divisors, or generalize the logic to find amicable numbers — pairs of numbers such that the sum of the divisors of one is the other.

Extensions and Optimization
Instead of iterating to n/2, iterate to sqrt(n), incrementing both i and n/i to the sum when i divides n. This decreases runtime dramatically for big inputs. Precompute divisor sums for lots of numbers if the problem has several queries. This optimization technique exposes you to sieve-like methods and caching practices in programming.

How It Shows Up in Actual Projects
Although explicit perfect number testing is uncommon in production, the underlying method — looping over factors and summing results — is central in cryptography, number theory research, and performance-critical applications with factorization or formulas involving divisors. Checksum algorithms or cryptographic key generation, for instance, occasionally depend on prime factor and divisor sum properties at times.

Common Pitfalls
Novices usually make the error of adding the number itself into the summation of divisors, thus every number would seem abundant. Another error is to begin from 0 (division by zero) or 2 (omission of divisor 1). Forgetting to consider small numbers such as 1 (not perfect) is also a common mistake.

SEO-Optimized Paragraph
A perfect number is a positive integer equal to the sum of its proper divisors, making it a classic problem in number theory and programming interviews. Writing an efficient perfect number program in C, C++, Java, or Python helps beginners master loops, conditionals, and divisor logic while improving problem-solving skills. Mastering how to identify perfect numbers also equips you for similar problems such as identifying abundant and deficient numbers, optimizing searches for divisors, and using concepts from number theory to solve coding contest problems, competitive programming, and real-world algorithmic design.