Have to create program that list all perfect number( sum of factors = number ) 1 - 1000.
- this is for a java class, need to only use "for" loops
I have checked my code 100 times and getting no output, I am missing a logical error somewhere, could someone help me out?
public static void main(String[] args)
{
// variables
int total = 0;
final int LIMIT = 1000;
// for loop to test all numbers 1-1000
for(int i = 1; i <= LIMIT; i++)
{
// if statement
if((i != 1) && (total == i - 1))
{
// prints perfect number
System.out.println((i - 1) + " is a perfect number");
// resets total value
total = 0;
}
// gets and add factors as total
for(int divider = 1; divider < i; divider++)
{
if((i % divider) == 0)
{
total += divider;
}
}
}
}