What is Perfect number in Java

A number is said to be perfect number if the sum of the factors (including 1 and excluding the number itself) is the same as the original number. For example: 6, 28, etc.

• Sample Input: 6
• Sample Output: It is a perfect number
• The factors of 6= 1,2,3 and 6
• Therefore including factor 1 and excluding 6 the logic goes: 1 + 2 + 3 = 6 which is equal to the original number

Let’s see another perfect number

• Sample Input: 28
• Sample Output: It is a perfect number
• The factors of 28=1, 2, 4, 7 and 28
• Therefore including factor 1 and excluding 28 the logic goes: 1 + 2 + 4 + 7 + 14 = 28 which is equal to the original number
``````// program to check whether a number is perfect or not

import java.util.Scanner;
class Perfect
{
public static void main(String args[])
{
int i,num,sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number");
num=sc.nextInt();
for(i=1;i<num;i++)
{
if(num%i==0)
sum=sum+i;
}
if(sum==num)
System.out.println(num+" is a perfect number");
else
System.out.println(num+" is not a perfect number");
}
}

``````

Output: