CHECK IF THE NUMBER IS PERFECT OR NOT

 EXAMPLES ----> 6 has factor 3,2,1 and 6. But a number is perfect when it is equal to the sum of its proper divisor excluding the number itself  as 6 = 3+2+1

package com.cnc;


public class Main {

public static void main(String[] args) {
System.out.println(isPerfectNumber(28));
}

public static boolean isPerfectNumber(int number) {
int sum = 0;
if (number < 1) return false;
else
for (int a = 1; a < number; a++) {
if ((number % a == 0)) {
sum = sum +a;
}
if(sum==number) return true;
} return false;
}

}  

Popular posts from this blog

NUMBER PALINDROME CHALLENGE

ENCAPSULATION CHALLENGE

USER INPUT PRINT ORIGINAL,REVERSE ARRAY AND SORTED ARRAY