# Perfect Number homework help

Page 1 of 1

## 10 Replies - 1018 Views - Last Post: 26 November 2011 - 09:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256812&amp;s=0f54eb74c754411e4da735d00aef51ac&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 djwendt

Reputation: 0
• Posts: 16
• Joined: 21-November 11

# Perfect Number homework help

Posted 22 November 2011 - 01:01 PM

I know there are other posts about this, but none of them really help me with my problem. The assignment is to create a program that will ask how many numbers you want to test, then have you input the numbers to test. My code is returning the remainder amount:
9:4
8:4
7:3
6:3
5:2
4:2
3:1
2:1
1:NOT PERFECT

I need it to also display the factors in descending order.

So here is my method to test:
```   public static boolean testPerfect(int number2)
{

int sum = 0;
boolean testing = false;

for (int i = 1; i < number2; i++ )
{
if ( i % number2 == 0)
sum = sum + i;
testing = true;

}  return testing;

} // end testPerfect

```

I know something is messed in my for loop due to the return of all numbers, except #1, to display factors/modulus.

This is my method to print factors, which I think is all correct except for displaying ALL factor in descending order.

```public static void printFactors(int print)
{
int total = 0;

for (int divisor = 1; divisor < print; ++divisor)
{
if(divisor == print/2)
{

System.out.printf("%d", divisor);

}

}
}

```

I know I need to decrement divisor, but when I do i get a infinite loop. Do I need to take the if statement out of the for loop?

As stated in the rules, I'm not looking for anyone to correct this for me just some help on where I need to fix my problems.

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Perfect Number homework help

### #2 macosxnerd101

• Self-Trained Economist

Reputation: 9037
• Posts: 33,523
• Joined: 27-December 08

## Re: Perfect Number homework help

Posted 22 November 2011 - 01:28 PM

It makes more sense to me that if print%divisor == 0 to print the divisor. Also, you could make sure your divisor < print/2. Now- do you want all the factors or all the prime factors of each number?

### #3 SwiftStriker00

• Microsoft Insider

Reputation: 429
• Posts: 1,596
• Joined: 25-December 08

## Re: Perfect Number homework help

Posted 22 November 2011 - 01:39 PM

Here is what a better print factors looks like:
```public static void printFactors( int number ){
for( int i = 1; i <= number /2; i ++ ) {

if( number % i == 0 ){
System.out.print( i + " " );
}
}

System.out.println( number );
}

```

What is this doing? It is stepping though each number up until our input number ( lets use 10 as an example ). We are going to start at 1, because we don't want to divide by zero. Then we don't need to go past half the input value, so we will continue until number / 2 . note, i used <= and not just =, because if number is an even number, we will want to check that too!

Now for inside the loop, what is a factor? something we can divide the input by evenly. Modulus is a great tool for this, by saying number % i , that will divide i by number and return the remainder. Well, if the remainder is 0, then we know it evenly divides into number.

Lastly we print out the number itself because, anything % 1 = anything.

### #4 djwendt

Reputation: 0
• Posts: 16
• Joined: 21-November 11

## Re: Perfect Number homework help

Posted 22 November 2011 - 03:11 PM

@macosxnerd101 - It needs to be all the factors.

Thanks for the help you two. Will try that after a bit and let you know how it goes.

### #5 djwendt

Reputation: 0
• Posts: 16
• Joined: 21-November 11

## Re: Perfect Number homework help

Posted 22 November 2011 - 07:23 PM

So that helped with the printing factors but I need them to be in descending order. My thinking was to decrement i (i--) but that got me nowhere(infinite loop).

Also it's printing the factors for non-perfect numbers. That has to do with my testPerfect method (right?), but what is wrong that makes it print incorrectly.

Example:
9: 1 3
8: 1 2 4
7: 1
6: 1 2 3
5: 1
4: 1 2
3: 1
2: 1
1: NOT PERFECT

Do I need to compare sum to number2, if they are the same then it would be true? If so, where? I've tried a separate if statement, but that didn't work.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8022
• Posts: 31,148
• Joined: 06-March 08

## Re: Perfect Number homework help

Posted 22 November 2011 - 07:36 PM

Complicated for nothing

```		for(int i = 2; i < 100; ++i) {
int total = 0;
System.out.print(i + ": ");
for(int j = 1; j < i-1; ++j) {
if(i % j == 0) {
System.out.print(" " + j);
total += j;
}
}
if(total == i)
System.out.print("    ......perfect");
System.out.println();
}

```

### #7 djwendt

Reputation: 0
• Posts: 16
• Joined: 21-November 11

## Re: Perfect Number homework help

Posted 22 November 2011 - 10:31 PM

I'm not sure what you mean with that post. I have a main method that I haven't posted that returns most everything, it calls testPerfect to calculate the sumation of the factors & calls printFactors (which is void) to print the factors if the number entered is perfect. There's something wrong with the testPerfect for loop, but I just can't see it. Also how do I print the factors in descending order? I've tried decrementing i, but that results in an infinite loop.

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8022
• Posts: 31,148
• Joined: 06-March 08

## Re: Perfect Number homework help

Posted 22 November 2011 - 10:39 PM

If we have to do everything...

```public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scan.nextInt();
testPerfect(number);
}

static void testPerfect(int i) {
int total = 0;
System.out.print(i + ": ");
int nbValue = 0;
int[] value = new int[500];
for(int j = 1; j < i-1; ++j) {
if(i % j == 0) {
value[nbValue++] = j;
total += j;
}
}
for(int j = nbValue; --j >= 0;)
System.out.print(" " + value[j]);
if(total == i)
System.out.print("    ......perfect");
System.out.println();
}

```

Happy coding

### #9 djwendt

Reputation: 0
• Posts: 16
• Joined: 21-November 11

## Re: Perfect Number homework help

Posted 23 November 2011 - 08:30 AM

Thank you for that but I can't use arrays on this assignment.
Here is what i have for a main

```public static void main ( String args[])
{

Scanner input = new Scanner ( System.in );

int number = 0;
int number2 = 0;

do
{
System.out.print("How many numbers would you like to test? ");
number = input.nextInt();
} while (number < 1);

for (int count = 0; count < number; ++count)
{
System.out.print("Please enter a possible perfect number: ");
number2 = input.nextInt();
testPerfect(number2);

if (testPerfect(number2)== true)
{
System.out.printf("%d: ", number2);
printFactors(number2);
System.out.println("");
}
else

{
System.out.printf("%d:NOT PERFECT\n", number2);
}

}

}

```

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8022
• Posts: 31,148
• Joined: 06-March 08

## Re: Perfect Number homework help

Posted 23 November 2011 - 10:01 PM

If you can't use array just no way you will remember the first divisor and print them in reverse order. You will have to store them somewhere

### #11 djwendt

Reputation: 0
• Posts: 16
• Joined: 21-November 11

## Re: Perfect Number homework help

Posted 26 November 2011 - 09:48 AM

So this is what I finally ended up with :

```import java.util.Scanner;

public class djwendt_Perfect
{
public static void main (String args [])
{
int number = 0;   // variable for how many to test
int number2 = 0;  // variable for possible perfect

Scanner input = new Scanner (System.in);

do
{
System.out.print("How many numbers would you like to test? ");
number = input.nextInt();
} while (number < 1); // end do...while

for (int count = 0; count < number; ++count)
{
System.out.print("Please enter a possible number: ");
number2 = input.nextInt();

if (testPerfect(number2))
{
System.out.printf("%d:", number2);
printFactors(number2);
System.out.println("");
} // end if statement
else
{
System.out.printf("%d:NOT PERFECT\n", number2);
} // end else
}// end for loop
} // end main
// method to test if number is Perfect
public static boolean testPerfect(int test)
{
int sum = 0;            // variable to hold sumation
boolean result = false; // variable

if (test == 0)
{
result = false;
}
for (int counter = 1; counter <= (test/2); ++counter)
{
if (test % counter == 0)
{
sum = sum + counter;
} // end if statement
} // end for loop

if  (sum == test)
{
result = true;

}return result; // end if statement
} // end testPerfect method
// method that prints the factors in descending order
public static void printFactors(int print)
{

int total = 0; // variable to hold mod number

for (int factors = print - 1; factors > 0; factors--)
{
total = print % factors;

if (total == 0)

System.out.print(factors + " ");

} // end for loop
}
}

```

I only have I minor problem, it's printing 0(zero) as a perfect number.