This question already has an answer here:
I was working on my assignment and I have to write a method that checks the given number is a perfect number or not and returns true or false. (Perfect numbers like 6, 28, 496) But my code always seems to return true. I cannot understand why, I would be happy if you helped me here is my code:
import acm.program.*;
public class assignment2 extends ConsoleProgram {
public void run(){
boolean x = isPerfect(19);
if (x=true){
println("Perfect");
}
else {
println("Not perfect");
}
}
public boolean isPerfect(int x){
int sum=0;
for (int i=1;i>=x;i++){
if (x%i==0){
sum=sum+i;
}
}
if (sum==x){
return true;
}
else {
return false;
}
}
}
Aucun commentaire:
Enregistrer un commentaire