0
0
JavaProgramBeginner · 2 min read

Java Program to Check Perfect Number with Output

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

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 numbers smaller than it that divide it evenly. Add these divisors together. If the total equals the original number, it is perfect; otherwise, it is not.
📐

Algorithm

1
Get the input number.
2
Initialize a sum to zero.
3
Loop from 1 to half of the number.
4
If the current number divides the input evenly, add it to the sum.
5
After the loop, compare the sum with the input number.
6
If they are equal, the number is perfect; otherwise, it is not.
💻

Code

java
import java.util.Scanner;

public class PerfectNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int sum = 0;
        for (int i = 1; i <= num / 2; i++) {
            if (num % i == 0) sum += i;
        }
        if (sum == num) System.out.println(num + " is a perfect number");
        else System.out.println(num + " is not a perfect number");
        sc.close();
    }
}
Output
6 is a perfect number
🔍

Dry Run

Let's trace input 6 through the code

1

Input number

num = 6, sum = 0

2

Loop from 1 to 3 (6/2)

Check divisors 1, 2, 3

3

Check divisor 1

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

4

Check divisor 2

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

5

Check divisor 3

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

6

Compare sum and number

sum = 6 equals num = 6, so print perfect

DivisorIs Divisor?Sum
1Yes1
2Yes3
3Yes6
💡

Why This Works

Step 1: Find divisors

We check all numbers less than or equal to half the input because no divisor (except the number itself) can be greater than half.

Step 2: Sum divisors

We add each divisor to a sum to find the total of all proper divisors.

Step 3: Compare sum to number

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

🔄

Alternative Approaches

Using while loop
java
import java.util.Scanner;

public class PerfectNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int sum = 0, i = 1;
        while (i <= num / 2) {
            if (num % i == 0) sum += i;
            i++;
        }
        if (sum == num) System.out.println(num + " is a perfect number");
        else System.out.println(num + " is not a perfect number");
        sc.close();
    }
}
Uses a while loop instead of for; functionally same but shows loop variety.
Using method to check perfect number
java
import java.util.Scanner;

public class PerfectNumber {
    public static boolean isPerfect(int num) {
        int sum = 0;
        for (int i = 1; i <= num / 2; i++) {
            if (num % i == 0) sum += i;
        }
        return sum == num;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        if (isPerfect(num)) System.out.println(num + " is a perfect number");
        else System.out.println(num + " is not a perfect number");
        sc.close();
    }
}
Separates logic into a method for better code reuse and clarity.

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

Time Complexity

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

Space Complexity

Uses only a few variables for sum and loop counters, so space is constant O(1).

Which Approach is Fastest?

All approaches check divisors similarly; using a method improves readability but not speed.

ApproachTimeSpaceBest For
For loopO(n)O(1)Simple and clear code
While loopO(n)O(1)Shows loop variety
Method extractionO(n)O(1)Code reuse and clarity
💡
Only check divisors up to half the number to save time.
⚠️
Including the number itself as a divisor, which always makes the sum larger.