# Perfect Number in Java

Here you will know about perfect number in Java with program and example.

What is Perfect Number?

The number said to be perfect number when sum of all positive divisors of that number excluding that number itself in divisors is equal to that number.

Or, sum of all positive divisors of that number including that number is 2 times of the number.

Key Points:

• The number of divisors of perfect number will be even.
• Most of the perfect numbers are even numbers.
• Odd numbers are very rare in perfect numbers.
• The digital root of all perfect numbers (except 6) is 1.

Example:

i) 28=2+8=10

1+0=1

ii) 496=4+9+6=19

1+9=10

1+0=1

This will be true for all perfect numbers.

Suppose take 6 is perfect number.

Positive divisors of 6 are 1, 2, 3, 6. We should exclude number in divisors so 6 we need to exclude. Then those divisors are called proper divisors.

Remaining numbers sum is 1+2+3=6

Number = sum of all proper positive divisors.

or

1+2+3+6=12

Number= ½(sum of all positive divisors).

## Program for Perfect Number in Java

```import java.io.*;
import java.util.*;
class Example
{
boolean perfect(int num) // method for checking the number is perfect or not.
{
int sum=0;
for(int i=1; i<num; i++) // here it self we are excluding the number in divisors
{
if (num%i==0)
{
sum =sum+i; //summing up all positive divisors.
}
}
if(sum==num)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])
{
int n;
boolean flag;
Scanner sc=new Scanner (System.in);
System.out.println ("enter any positive number ");
n = sc.nextInt();
Example ob= new Example();
flag = ob.perfect(n);
if (flag)
System.out.println (n +" is a perfect number ");
else
System.out.println (n +" is not a perfect number ");
}
}```

Output

enter any positive number
28
28 is a perfect number