Perfect number – a number is called a Perfect number if it is equal to the sum of its factors other
than the number itself. Example: 6 = 1 + 2 + 3.
Program:
import java.util.*;
class Perfect_Number
{
public static void main(String[] args)
{
int n,i,s=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
n=sc.nextInt();
for(i=0;i<n;i++)
{
if(i%2==0)
s=s+i;
}
if(n==s)
{
System.out.println("Entered number is perfect");
}
else
System.out.println("Entered number is not perfect");
}
}
Output:
Enter the number:
6
Entered number is perfect
Enter the number:
16
Entered number is not perfect