PERFECT NUMBER

PERFECT NUMBER:-no which is sum of its proper divisor(proper divisor:- no which completely divide with no remainder)

package perfect_number;

import java.util.Scanner;
public class Perfect_number
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int number = sc.nextInt();
int sum = 0;
for (int i = 1; i < (number - 1); i++)
{
int divisorcheck = number % i;
if (divisorcheck == 0)
sum = sum + i;
}
if (sum == number)
System.out.println("perfect number : " + number);
else
System.out.println("non-perfect " + number);
}
}
/**documentation comment
* just make a loop before that input number and check for number divisibility by modulus operation with all succeeding number
* if modulus satisfies then sum all those satisfying no and compare with the given entered number
*/

                          OUTPUT

Enter the number
6
perfect number : 6

No comments:

Post a Comment