Write a java program to check the perfect number

Java program to check the perfect number. Write a program to that takes a positive integer and returns true if the number is perfect number. A positive integer is called a perfect number if the sum of all its factors (excluding the number itself, i.e., proper divisor) is equal to its value.

For example, the number 6 is perfect because its proper divisors are 1, 2, and 3, and 6=1+2+3; but the number 10 is not perfect because its proper divisors are 1, 2, and 5, and 1+2+5 is not equal to 10

Input and Output Format:

Input consists of a integer.
Output consists of TRUE / FALSE.
Refer sample output for formatting specifications.

Sample Input 1:
28

Sample Output 1:
TRUE

java program to check the perfect number

package com.demo;
 
import java.util.*;
 
public class Main {
 
public static Scanner sc;
 
public static void main(String[] args) {
int n=28;
System.out.println(perfectNumber(n));
}
public static boolean perfectNumber(int n) {
int n1=0;
boolean b=false;
for(int i=1;i<n;i++)
if(n%i==0)
n1+=i;
System.out.println(n1);
if(n1==n)
b=true;
return b; }
 
}

Add a Comment

Your email address will not be published. Required fields are marked *

3 × one =