// This program tests if a number is a perfect number import java.util.Scanner; public class Perfect { public static void main ( String args []) { Scanner i = new Scanner ( System.in ); int number = 0; int count = 1; int test; while (number <=0) { System.out.print("How many numbers would you like to test? "); number = i.nextInt(); } System.out.print("\n"); while (count <= number) { System.out.print("Please enter a possible perfect number: "); test = i.nextInt(); System.out.printf("%d:", test); if ( testPerfect(test) == true ) { printFactors(test); System.out.print("\n"); } else { System.out.println("NOT PERFECT"); } System.out.print("\n"); count++; } } /** * This method checks if a number entered by the user is perfect * * @param test The desired number to be tested if perfect * @return True if test is a perfect number, false if not */ public static boolean testPerfect( int test ) { int sum = 0; boolean perfect = false; int count2 = 1; while (count2 < test) { if (test % count2 == 0) { sum+=count2; } count2++; } if (sum == test && test != 0) { perfect = true; } return perfect; } /** * This method prints the factors of the perfect number * * @param test The perfect number to print factors of */ public static void printFactors( int test ) { int count3 = test - 1; while (count3 >0) { if (test % count3 == 0) { System.out.printf("%d ",count3); } count3--; } } }