Thursday 16 February 2017

Perfect number program

Q : Write a program to check whether a number is perfect or not ?

A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. 

Example : 
6 = 1 + 2 + 3, 
28 = 1 + 2 + 4 + 7 + 14, 
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 

Solution :


class perfect {
 void check(int n) {
  int n, s = 0, d;
  for (d = 1; d < n; d++) {
   if (n % d == 0)
    s = s + d;
  }
  if (s == n)
   System.out.println("Perfect Number");
  else
   System.out.println("Not a Perfect Number");
 }
}

No comments:

Post a Comment