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
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

dpearsonCommented:
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
gudii9Author 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));
    }
}

Open in new window


i got

It is a perfect number
Is perfect number: true
dpearsonCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gudii9Author 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));
    }
}

Open in new window


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
gudii9Author 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
dpearsonCommented:
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);
	}

Open in new window

gudii9Author Commented:

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

oops.. i miss this.
gudii9Author 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
gudii9Author 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
gudii9Author Commented:
496=62*8
gudii9Author 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


is 0 is perfect number? please advise
dpearsonCommented:
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;
					factors.add(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);
			}
		}
	}

Open in new window


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
dpearsonCommented:
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
gudii9Author 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;
                factors.add(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);
    }
}

Open in new window


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?
please advise
gudii9Author 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?
gudii9Author 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;
				factors.add(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);
		}
	}
}
}

Open in new window


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 ;

Open in new window

does null has any impact later on?

how factores defined in main method
List<Integer> factors = ipn.getPerfectNumberFactors(i) ;

Open in new window

can be seen in called method as below?

	if (isPerfect)
			return factors ;

Open in new window



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

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

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

please advise
dpearsonCommented:
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

Your issues matter to us.

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

Start your 7-day free trial
gudii9Author 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;
				fact.add(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);
		}
	}
}

}

Open in new window


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;
				fact.add(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);
		}
	}
}

}

Open in new window


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]
dpearsonCommented:
That looks perfect to me :)
ozoCommented:
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.