Nick Ward
Nick Ward

Reputation: 11

Calculating Perfect Numbers

See, I have a boolean method that gets the divisors that work by moding the number by a divisor that is checked and determined true by a for loop (not the first one, that just loops the program for a determined amount of input. I'm not sure if there's some way to take the multiple results of the loop and add them together, but that is what I need to do. right now I'm displaying the results of the loop but that was for debugging, in the end the output will be whether it is abundant (the added divisors are over the number), perfect (the added divisors equal the number) or deficient (the added divisors are less then the number) This is the code in eclipse:

import java.util.*;
public class PerfectNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        for(int i = 0; i < 15; i++)
        {
            Scanner reader = new Scanner(System.in);
            int number = 0;
            int divisor = 1;
            int addnum = 0;
            System.out.println("Please input a number to check if it is perfect, abundant, or deficient");
            number = reader.nextInt();

            for(divisor = 1; divisor < number; divisor++)
            {
                isDivisor(number, divisor);
                if(isDivisor(number, divisor) == true)
                {
                    System.out.println(divisor);
                }
            }
        }   
    }

    static boolean isDivisor(int number, int divisor)
    {
        if (number % divisor == 0)
            return true;
        else 
            return false;
    }

}

Upvotes: 0

Views: 2143

Answers (3)

user987339
user987339

Reputation: 10707

You don't need isDivisor because it is equal to the following expression:

number % divisor == 0

Just sum up divisors:

int  numDivSum = 0;       

for(divisor = 1; divisor < Math.sqrt(number); divisor++)
{

    if(number % divisor == 0)
    {
        numDivSum += divisor;
    }
}

and check is the numDivSum is perfect, abundant, or deficient.

Upvotes: 1

Vogel612
Vogel612

Reputation: 5647

If you want to sum things in a loop you can use some statement like this:

Int sum;
// your for loop start
    If ( isDivisor(number, divisor))
        sum += divisor;
   // end of loop
   //here you can do the comparison.

There is no need to compare to true in your if-statement. Also this codesnippet only works, if you have only one statement in the if body. If you need to do multiple things, you have to wrap that whole thing in brackets.

Also your first call to isDivisor is useless, as you are not doing anything with the value you get.

Upvotes: 0

MikeRaines
MikeRaines

Reputation: 11

To answer what seems to be your immediate question for what smells like a homework problem ;)

I'm not sure if there's some way to take the multiple results of the loop and add them together

You can check the return of your function as you're already doing and increment a counter

public boolean isSeven(int x){
    return x == 7;
}

public static void main(String[] args){
    int sumOfSevens= 0;
    int i = 0;
    while(i < 10){
        if(isSeven(7)){
            sumOfSevens = sumOfSevens + 7; // or +=
            ++i;
        }
    }
    // At this point sumOfSevens = 70;
} 

Upvotes: 1

Related Questions