I need help writing this in java A perfect number p is a po

I need help writing this in java.

A perfect number, p , is a positive integer that equals the sum of its divisors, excluding p itself. For example, 6 is a perfect number because the divisors of 6 (1, 2, and 3) sum to 6. Write a program that prints all perfect numbers less than 1000. There are not many!

Solution

PerfectNumbers.java


public class PerfectNumbers {

  
   public static void main(String[] args) {
       System.out.println(\"Perfect Numbers are : \");
       for(int i=1; i<1000; i++){
           if(isPerfect(i)){
               System.out.print(i+\" \");
           }
       }
   }
   public static boolean isPerfect(int n) {
       int sum = 0;
       for (int i = 1; i <= n / 2; i++) {
           if (n % i == 0) {
               sum += i;
           }
       }
       if (sum == n) {
           return true;
       } else {
           return false;
       }
   }

}

Output:

Perfect Numbers are :
6 28 496

I need help writing this in java. A perfect number, p , is a positive integer that equals the sum of its divisors, excluding p itself. For example, 6 is a perfe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site