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.
- 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.
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.
Number= ½(sum of all positive divisors).
Program for Perfect Number in Java
boolean perfect(int num) // method for checking the number is perfect or not.
for(int i=1; i<num; i++) // here it self we are excluding the number in divisors
sum =sum+i; //summing up all positive divisors.
public static void main(String args)
Scanner sc=new Scanner (System.in);
System.out.println ("enter any positive number ");
n = sc.nextInt();
Example ob= new Example();
flag = ob.perfect(n);
System.out.println (n +" is a perfect number ");
System.out.println (n +" is not a perfect number ");
enter any positive number
28 is a perfect number