# Perfect Number Problem :)

Page 1 of 1

## 11 Replies - 1711 Views - Last Post: 20 February 2012 - 02:59 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=267296&amp;s=cd2c1bf15f49a6c7ddeaa7e24e9373f2&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 harrypotter2

Reputation: -1
• Posts: 24
• Joined: 19-February 12

# Perfect Number Problem :)

Posted 19 February 2012 - 10:44 AM

Hi,
I'm new a java and I was wondering what is wrong with this code here:
1) It says here that "increment" cannot be resolved to a type what does this mean
2) I don't have a clue what to do afterwards in order to figure out perfect numbers like 6 and such.

```public class Perfect {
public static void main(String[] args){
int counter1;
int counter2;
while(counter1 <= 1000);
counter1++;
while (counter2 < counter1);
if (counter2 == 0);
increment total;
if (total = counter1);
System.out.print("It is a perfct number");
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Perfect Number Problem :)

### #2 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• Joined: 14-March 10

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:05 AM

A question is what did you mean to do with this statement?
```increment total;
```

Total in not declared and also do increment.

### #3 harrypotter2

Reputation: -1
• Posts: 24
• Joined: 19-February 12

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:50 AM

OK I did what you told me but now I got a different error on line 10. Here it tells me a type mismatch what am I supposed to do?

```if (total1 = counter1);
```

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:52 AM

= for assignment, == for comparison

Also, lose the ; on the lines with ifs and whiles, or when (or if) you get this to run you'll have a whole new set of problems.

### #5 smohd

• Critical Section

Reputation: 1824
• Posts: 4,627
• Joined: 14-March 10

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:56 AM

First fix what JackOfAllTrades told you, then post our updated code when you do changes.

### #6 harrypotter2

Reputation: -1
• Posts: 24
• Joined: 19-February 12

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 12:11 PM

What else am I supposed to do after this? and, How do I get it to print every perfect number from 1 to 1000 because I do not know how to?

```public class Perfect {
public static void main(String[] args){
int counter1 = 0;
int counter2 = 0;
while(counter1 <= 1000){
counter1++;
}
while (counter2 < counter1){
if (counter2 == 0){
int increment;
int total1 = 0;
if (total1 == counter1){
System.out.print("It is a perfct number");

}
}
}
}
}
```

### #7 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 02:40 PM

Perhaps we should step back and consider the logic here. Your job is write a program to print all the perfect numbers from 1 to 1000, right? A perfect number is defined by wikipedia to be "a positive integer that is equal to the sum of its proper positive divisors." At this point I already see a way you can make this program easier on yourself. You can create a method to determine if a number is perfect.

boolean isPerfect(int number) { }

Now, that is for you to figure out as part of your assignment but let's see how this could help us in our main method:
```public static void main(String[] args) {
int iterations = 1000; // what number will we go to? 1000
for(int i = 1; i < iterations+1; i++) { // Loop 1000 times starting at 1
if( isPerfect(i) ) {
System.out.println(i + " is a perfect number!"); // Display that this number is perfect
}
}
}
```
That main method looks very simple and all you have to do now is figure out how to tell if a number is perfect or not. Do you think you could handle that?

This post has been edited by Sheph: 19 February 2012 - 02:41 PM

### #8 harrypotter2

Reputation: -1
• Posts: 24
• Joined: 19-February 12

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 04:18 PM

Yeah but now i get a syntax error.

```public class Perfect {
public static void main(String[] args){
int counter1 = 0;
int counter2 = 0;
boolean isPerfect(int number);
int iterations = 1000; // what number will we go to? 1000
for(int i = 1; i < iterations+1; i++)
if( isPerfect(i) ) {
System.out.println(i + " is a perfect number!"); // Display that this number is perfect
}
while(counter1 <= 1000){
counter1++;
while (counter2 < counter1){
if (counter2 == 0){
int increment;
int total1 = 0;
if (total1 == counter1){
System.out.print("It is a perfct number");

}
}
}
}
}
```

### #9 The_Programmer-

• Paranormal Investigator

Reputation: 25
• Posts: 694
• Joined: 24-October 11

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 05:05 PM

You should read a book on Java or go to thenewboston.com and watch the Java tutorials. It seems that you have not learned much Java. In a normal class(this means non-abstract classes) you can not just write
```boolean isPerfect(int number);
```
You need to have a method body like this
```boolean isPerfect(int number) {
//do stuff here and return a boolean value based on if the number is a perfect number.

}

```

### #10 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: Perfect Number Problem :)

Posted 19 February 2012 - 05:58 PM

A reference from the snippet section.

### #11 harrypotter2

Reputation: -1
• Posts: 24
• Joined: 19-February 12

## Re: Perfect Number Problem :)

Posted 20 February 2012 - 11:40 AM

Now heres my next problem. what should i put in the curly brackets? Give me an example or somthing.

```public class Perfect {
public static void main(String[] args){
boolean isPerfect (int number){

}
int iterations = 1000; // what number will we go to? 1000
for(int i = 1; i < iterations+1; i++) { // Loop 1000 times starting at 1
if(isPerfect(i)){
System.out.println(i + " is a perfect number!"); // Display that this number is perfect

}
}
}
}
```

### #12 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

## Re: Perfect Number Problem :)

Posted 20 February 2012 - 02:59 PM

boolean isPerfect() is a method, you need to declare it outside of main(), and if you want to use it in main without creating an object, then you have to declare it as "static". Since you don't know what an object is probably, then I suggest you keep it static for now. Inside the curly brackets should test to see if the variable number is a perfect number. You have basically copy and pasted the code I wrote so far, so let's see some attempts on your part, even if they are wrong. If you show me you can come up with something on your own, I would be glad to continue to help you.