0

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

}
false
  • 10,264
  • 13
  • 101
  • 209
Nick Ward
  • 11
  • 5
  • To clarify, you mean if `isDivisor` returns true, you want that divisor (not number) to be added to all other true `isDivisor` numbers? – hichris123 Dec 05 '13 at 00:50

3 Answers3

1

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;
} 
MikeRaines
  • 11
  • 3
1

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.

user987339
  • 10,519
  • 8
  • 40
  • 45
0

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.

Vogel612
  • 5,620
  • 5
  • 48
  • 73