56. JAVA Program to find the given number is Perfect number or Not.
How does this program work?
- In this program you are going to learn about to check if a number is Perfect number or not using JAVA.
- A perfect number is a number if it is equal to the sum of its factors, which means original number is equal to sum of all its factors except the number itself.
- If sum is equal to the original number, then that number is perfect number else the number is not a perfect number.
Here is the code
//Perfect Number or not
import java.util.Scanner;public class Perfect
{
public static void main(String[] args )
{
int i, n, sum = 0 ;
System.out.println(" Enter any Number: ");
Scanner obj=new Scanner(System.in ); ;
n = obj.nextInt();
for(i = 1 ; i < n ; i++)
{
if(n% i == 0)
{
sum = sum + i;
}
}
if (sum == n)
{
System.out.println( n+ " is a Perfect Number");
}
else
{
System.out.println( n+ " is not a Perfect Number");
}
}
}