I’m trying to make a program calculate the first four perfect numbers. It compiles, but when I run the program, the for-loops seem to be invisible. Like when I look at the debugger, it looks like the for-loops are just skipped over and the variables don’t exist.
public static void main(String[] args)
{
int pNum = 2; // starting number to check
int sum = 0;
for (int count = 1; count <= 4; count++)
{
for (int factor = 1; factor < pNum; factor++)
{
if (pNum % factor == 0)
sum += factor;
}
if (pNum == sum)
{
System.out.println(pNum + " is a perfect number.");
count++;
}
pNum++; sum = 0;
}
}
1 Answer