public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int x, sum, i;
x = input.nextInt();
sum = 0;
for ( i = 1; i < x; i++ )
if ( x%i == 0 ) // i is a divisor of x
sum = sum + i; // Add i to sum
if ( sum == x )
System.out.println("is a perfect number");
else
System.out.println("is not a perfect number");
}
|
public class Tools
{
public static boolean isPerfect(int n)
{
...
}
}
|
In another class (e.g.: MyProg), write a main( ) method that calls the isPerfect( ) in the Tools class and prints out all the perfect numbers that are between 1 and 1,000,000.
public class MyProg
{
public static void main(String[] args)
{
...
// Must use the isPerfect( ) methid in the Tools class
}
}
|
Your program is correct if you see these 4 numbers printed: 6, 28, 496 and 8128