# PerfectNumber

Page 1 of 1

## 2 Replies - 34255 Views - Last Post: 29 September 2007 - 07:06 PMRate 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=34269&amp;s=28983e26e7651c9ceb71f491e826f104&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dontKnowJava

Reputation: 1
• Posts: 221
• Joined: 29-September 07

# PerfectNumber

Posted 29 September 2007 - 06:02 PM

```public class aa
{
public static void main(String args[])
{
System.out.println("Perfect numbers from 1 to 650");
test1();
}

public static void test1()
{
int divisor;
int number = 1;
int sum = 0;
for(divisor=1; divisor<number; divisor++)
{
if(number%divisor==0)
{
sum = sum + divisor;
}
}
if(sum==number)
{
System.out.println(number + " is a perfect number");
}
}
}
```

So what i need to do is find all perfect numbers between 1 and 650. A perfect number is a number whose divisors(except the number itself) equal to the original number. For example, divisors of 6 are 1,2, and 3. 1+2+3=6 so 6 is a perfect number. i figured out how to get the divisors of a number and add them together, but for some reason i cant increment to next number. If i set variable "number" in my formula for example to 6 it will print. I've tried putting an outer for and while loops and try to set outer loop counter to = number but it says variable not found. I was wondering is there a way to make a variable accessible to both outer and inner loop? Any help would be great. Thanks.

This post has been edited by PennyBoki: 30 September 2007 - 06:33 AM

Is This A Good Question/Topic? 0

## Replies To: PerfectNumber

### #2 Martyr2

• Programming Theoretician

Reputation: 5053
• Posts: 13,618
• Joined: 18-April 07

## Re: PerfectNumber

Posted 29 September 2007 - 06:40 PM

Hi there,

Good work on the program. I had to modify only a few things. What you were missing was a loop to loop through the numbers. So notice the for statement in the main function to do this. You also needed to pass the number to your function for testing. The last thing I did was removed the divisor variable declaration and just put it right in the for statement. I did this because you don't use it anywhere outside the loop. So keep it as local as possible. It makes it more readable and cleaner looking. Removes a line of code and makes your for statement nice.

Here is the finished product. Test it and you should see it finds your perfect numbers perfectly!

```public class aa
{
public static void main(String args[])
{
System.out.println("Perfect numbers from 1 to 650");

// Loop through the numbers between 1 and 650
for (int i = 1; i <= 650; i++) {
// Pass the number to our function for testing.
test1(i);
}
}

// Notice how we pass the number to the function for then testing
// in the for statement.

public static void test1(int number)
{
int sum = 0;

// No need to create the divisor variable outside the for statement
// divisor can be declared right in the for statement.

for(int divisor=1; divisor < number; divisor++)
{
if ((number % divisor) ==0)
{
sum = sum + divisor;
}
}

if(sum==number)
{
System.out.println(number + " is a perfect number");
}
}
}

```

I put a few in code comments that show you were I made some of the changes. Keep it up and enjoy the programming!

Enjoy!

At DIC we be coding ninjas!

### #3 dontKnowJava

Reputation: 1
• Posts: 221
• Joined: 29-September 07

## Re: PerfectNumber

Posted 29 September 2007 - 07:06 PM

Martyr2, on 29 Sep, 2007 - 06:40 PM, said:

Hi there,

Good work on the program. I had to modify only a few things. What you were missing was a loop to loop through the numbers. So notice the for statement in the main function to do this. You also needed to pass the number to your function for testing. The last thing I did was removed the divisor variable declaration and just put it right in the for statement. I did this because you don't use it anywhere outside the loop. So keep it as local as possible. It makes it more readable and cleaner looking. Removes a line of code and makes your for statement nice.

Here is the finished product. Test it and you should see it finds your perfect numbers perfectly!

```public class aa
{
public static void main(String args[])
{
System.out.println("Perfect numbers from 1 to 650");

// Loop through the numbers between 1 and 650
for (int i = 1; i <= 650; i++) {
// Pass the number to our function for testing.
test1(i);
}
}

// Notice how we pass the number to the function for then testing
// in the for statement.

public static void test1(int number)
{
int sum = 0;

// No need to create the divisor variable outside the for statement
// divisor can be declared right in the for statement.

for(int divisor=1; divisor < number; divisor++)
{
if ((number % divisor) ==0)
{
sum = sum + divisor;
}
}

if(sum==number)
{
System.out.println(number + " is a perfect number");
}
}
}

```

I put a few in code comments that show you were I made some of the changes. Keep it up and enjoy the programming!

Enjoy!

At DIC we be coding ninjas!

Thanks for help!

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }