# java perfect number

Hi,

I am goring through below link
http://java2novice.com/java-interview-programs/perfect-number/

what is perfect number
6=1*2*3 (so 1+2+3 =6 is perfect number which kind of make sense)
how 28 is perfect number? what are other perfect number. what is the logic i have to follow to find them, please advise
LVL 7
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
A perfect number is one where its factors (the number which divide into it), can be added together and you end up with the number itself.

So the factors of 28 are:
14 7 4 2 1
and if you add those together, you get...28

So it's a perfect number.

So the logic to find them is:
a) For each number from 1 to n/2 see if 1 divides exactly into n.
(Check the isPrime code from the earlier question for how you do this part)
b) If the value divides exactly into n, then it's a factor, so sum up those values.
c) See if the sum equals n or not.

The code you posted does this - so knowing these steps you should be able to map them back to the code.

Doug
Author Commented:
It is clear now. how do i extend this program to find all perfect numbers between1, 1000

``````
public class IsPerfectNumber {

public boolean isPerfectNumber(int number){

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number){
System.out.println("It is a perfect number");
return true;
} else {
System.out.println("It is not a perfect number");
return false;
}
}

public static void main(String a[]){
IsPerfectNumber ipn = new IsPerfectNumber();
System.out.println("Is perfect number: "+ipn.isPerfectNumber(28));
}
}
``````

i got

It is a perfect number
Is perfect number: true
Commented:
Write a loop from 1 to 1000 and call isPerfectNumber for each value.  Print out the number if the method returns true.

(You'll probably want to remove the existing print statements inside the method).

Doug
Author Commented:
``````
public class IsPerfectNumberLoop {

public boolean isPerfectNumber(int number){

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number){
System.out.println("It is a perfect number");
return true;
} else {
System.out.println("It is not a perfect number");
return false;
}
}

public static void main(String a[]){

IsPerfectNumberLoop ipn = new IsPerfectNumberLoop();
for(int i=0;i<=1000;i++)
System.out.println("Is perfect number: "+ipn.isPerfectNumber(i));
}
}
``````

i got below output with 2002 lines

It is a perfect number
Is perfect number: true
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is a perfect number
Is perfect number: true
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is a perfect number
Is perfect number: true
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is a perfect number
Is perfect number: true
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false
It is not a perfect number
Is perfect number: false

i wonder how to see the i corresponding number. also why it is displaying 2002 lines not 1000
Author Commented:
System.out.println("It is a perfect number"+i);

i is giving compilation error if i put 'i'.

how to pass 'i' to above from below so that i can print in sysout
Commented:
You didn't take my advice to remove the other print statements...

Anyway try this code:
``````	public static class IsPerfectNumberLoop {

public boolean isPerfectNumber(int number){

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
boolean isPerfect = (temp == number) ;
return isPerfect ;
}
}

public static void main(String a[]){

IsPerfectNumberLoop ipn = new IsPerfectNumberLoop();
for(int i=1;i<=1000;i++)
if (ipn.isPerfectNumber(i))
System.out.println("Is perfect number: "+i);
}
``````
Author Commented:

(You'll probably want to remove the existing print statements inside the method).

oops.. i miss this.
Author Commented:
public class IsPerfectNumberLoop {

public boolean isPerfectNumber(int number){

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
boolean isPerfect = (temp == number) ;
return isPerfect ;
}

public static void main(String a[]){

IsPerfectNumberLoop ipn = new IsPerfectNumberLoop();
for(int i=1;i<=1000;i++)
if (ipn.isPerfectNumber(i))
System.out.println("Is perfect number: "+i);
}

}

i have to remove static and move one bracket to down.

Now it is giving all three
Is perfect number: 6
Is perfect number: 28
Is perfect number: 496
Author Commented:
Is perfect number: 6--->1+2+3=6//this make sense

Is perfect number: 496---->2+8+31!=496//how this is perfect number
Author Commented:
496=62*8
Author Commented:

public class IsPerfectNumberLoop {

public boolean isPerfectNumber(int number){

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number){
// System.out.println("It is a perfect number");
return true;
} else {
// System.out.println("It is not a perfect number");
return false;
}
}

public static void main(String a[]){

IsPerfectNumberLoop ipn = new IsPerfectNumberLoop();
for(int i=0;i<=1000;i++)
if(ipn.isPerfectNumber(i))
System.out.println("Is perfect number: "+i);
}
}

when i tried starting 0 i got

Is perfect number: 0
Is perfect number: 6
Is perfect number: 28
Is perfect number: 496

Commented:
Remember a number is perfect when "all of its" factors sum to its value.

So for 496 one of its factors is "2".  Another is "4".  Anything which divides into it evenly is a factor.

If you're not sure what the factors are, we can modify the program we have quite easily to have it tell us.
Try this:

``````	public static class IsPerfectNumberLoop {

public List<Integer> getPerfectNumberFactors(int number){

List<Integer> factors = new ArrayList<>() ;

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
boolean isPerfect = (temp == number) ;

if (isPerfect)
return factors ;
return
null ;
}
}

public static void main(String a[]){

IsPerfectNumberLoop ipn = new IsPerfectNumberLoop();
for(int i=1;i<=1000;i++) {
List<Integer> factors = ipn.getPerfectNumberFactors(i) ;
if (factors != null) {
System.out.println("Perfect number: "+i + " has factors " + factors);
}
}
}
``````

0 is not a perfect number because perfect numbers refer to "positive" integers and so the first value that could be is 1.  (0 is neither positive, nor negative in this context).

Doug
Commented:
In case something gets lost in translation this is the output you should get:

Perfect number: 6 has factors [1, 2, 3]
Perfect number: 28 has factors [1, 2, 4, 7, 14]
Perfect number: 496 has factors [1, 2, 4, 8, 16, 31, 62, 124, 248]

Doug
Author Commented:
``````import java.util.ArrayList;
import java.util.List;

public class IsPerfectNumberLoop {

public boolean isPerfectNumber(int number){
List factors=new ArrayList();
int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;

}
}
if(temp == number){
System.out.println("It is a perfect number");
return true;
} else {
System.out.println("It is not a perfect number");
return false;
}
}

public static void main(String a[]){

IsPerfectNumberLoop ipn = new IsPerfectNumberLoop();
for(int i=0;i<=1000;i++)
System.out.println("Is perfect number: "+ipn.isPerfectNumber(i)+"factors are--"+factors);
}
}
``````

when i write as above factors is giving compilation error. Do i need to write 'factos' near calling method or inside called method like your code?
how to decide?
Author Commented:
how do i design my program based on requirement  in terms of what sequence of steps to write and what methods to write (here i can choose IsPerfectNumberLoop and getPerfectNumberFactors as separate methods also) out of different options?
Author Commented:
``````package comparator;

import java.util.ArrayList;
import java.util.List;

public class IsPerfectNumberLoop {

public List<Integer> getPerfectNumberFactors(int number){

List<Integer> factors = new ArrayList<>() ;

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
boolean isPerfect = (temp == number) ;

if (isPerfect)
return factors ;
return
null ;
}

public static void main(String a[]){

IsPerfectNumberLoop ipn = new IsPerfectNumberLoop();
for(int i=1;i<=1000;i++) {
List<Integer> factors = ipn.getPerfectNumberFactors(i) ;
if (factors != null) {
System.out.println("Perfect number: "+i + " has factors " + factors);
}
}
}
}
``````

i got output
Perfect number: 6 has factors [1, 2, 3]
Perfect number: 28 has factors [1, 2, 4, 7, 14]
Perfect number: 496 has factors [1, 2, 4, 8, 16, 31, 62, 124, 248]

``````	return
null ;
``````
does null has any impact later on?

how factores defined in main method
``````List<Integer> factors = ipn.getPerfectNumberFactors(i) ;
``````
can be seen in called method as below?

``````	if (isPerfect)
return factors ;
``````

i see factors defined 2 times as below. I wonder why

List<Integer> factors = new ArrayList<>() ;

List<Integer> factors = ipn.getPerfectNumberFactors(i) ;

Commented:
does null has any impact later on?
Yes - this null is how we indicate that a number is not prime.  If it's prime you get a list of factors.  Otherwise you get null.

i see factors defined 2 times as below. I wonder why
List<Integer> factors = new ArrayList<>() ;
List<Integer> factors = ipn.getPerfectNumberFactors(i) ;
The fact that these variables happen to have the same name has no impact on anything.

A variable's name has a "scope".  That is where you can use that name.
For most local variables (these are local variables) then the scope is the block of code where it's defined.
For a variable defined in a method - the name can only be used within that method.

So in this case, those two variables are both defined in different methods.  So the names don't interact at all.

Doug

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
``````import java.util.ArrayList;
import java.util.List;

public class IsPerfectNumberLoop2 {

public List<Integer> getPerfectNumberFactors(int number){

List<Integer> fact = new ArrayList<>() ;

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
boolean isPerfect = (temp == number) ;

if (isPerfect)
return fact ;
return
null ;
}

public static void main(String a[]){

IsPerfectNumberLoop2 ipn = new IsPerfectNumberLoop2();
for(int i=1;i<=1000;i++) {
List<Integer> factors = ipn.getPerfectNumberFactors(i) ;
if (factors != null) {
System.out.println("Perfect number: "+i + " has factors " + factors);
}
}
}

}
``````

i forgot about scope. I changed the varialbes names as below and tested successfully

``````import java.util.ArrayList;
import java.util.List;

public class IsPerfectNumberLoop2 {

public List<Integer> getPerfectNumberFactors(int number){

List<Integer> fact = new ArrayList<>() ;

int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
boolean isPerfect = (temp == number) ;

if (isPerfect)
return fact ;
return
null ;
}

public static void main(String a[]){

IsPerfectNumberLoop2 ipn = new IsPerfectNumberLoop2();
for(int i=1;i<=1000;i++) {
List<Integer> factors = ipn.getPerfectNumberFactors(i) ;
if (factors != null) {
System.out.println("Perfect number: "+i + " has factors " + factors);
}
}
}

}
``````

Perfect number: 6 has factors [1, 2, 3]
Perfect number: 28 has factors [1, 2, 4, 7, 14]
Perfect number: 496 has factors [1, 2, 4, 8, 16, 31, 62, 124, 248]
Commented:
That looks perfect to me :)
Commented:
Since all even perfect numbers are of the form (2^p-1)*2^(p-1), where p is prime,
and any odd perfect numbers, if they exist, are larger than Integer.MAX_VALUE
it suffices to check
public static boolean isEvenPerfectNumber(Long number){
if( (number&1) != 0 ){ return false; }
Long temp=(number&(-number));
number = (number/temp);
if( number!=temp*2-1 ){ return false; }
for( Long i=3l;i*i<number; i+=2 ){
if( number%i==0 ){ return false; }
}
return true;
}
which can verity 2305843008139952128 faster than the divisor test can verify 33550336
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.