Perfect number
--------------------
In number theory, a perfect number is a positive integer that is equal
to the sum of its positive divisors, excluding the number itself.
For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6,
so 6 is a perfect number.
Other perfect numbers are 28, 496, and 8,128.
//Source Code
import java.util.Scanner;
public class PerfectNumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int num , sum =0,i;
System.out.print("Enter a Number To Check : ");
num = in.nextInt();
for(i=1;i<=num/2;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==num)
{
System.out.print("It is a Perfect Number . ");
}
else
{
System.out.print("It is Not a Perfect Number . ");
}
}
}
// Sample Input :
Enter a Number To Check : 6
// Sample Output:
It is a Perfect Number .
// Sample Input :
Enter a Number To Check : 28
// Sample Output:
It is a Perfect Number .
// Sample Input :
Enter a Number To Check : 10
// Sample Output:
It is Not a Perfect Number .
0 Comments