An integer number is said to be a perfect number if it is equal to the sum of its factors (divisors), including 1 (but not the number itself). For example, 6 is a perfect number because 6 = 3+2+1. Write a method called isPerfect that returns true if the input integer number is a perfect number and false otherwise. Then, call this method in the main method that determines and prints all the perfect numbers between 2 and 2000. WRITE A JAVA PROGRAM.
//PerfectNumbersInRange.java public class PerfectNumbersInRange { public static boolean isPerfect(int num){ int sum = 0; for(int i = 1;i<num;i++){ if(num%i == 0){ sum += i; } } return sum==num; } public static void main(String[] args) { for(int i =2;i<=2000;i++){ if(isPerfect(i)){ System.out.println(i); } } } }
Get Answers For Free
Most questions answered within 1 hours.