Perfect Number in JAVA

 ✹ A number is said to be a perfect number if the sum of its positive divisors excluding the number itself is equal to that number

For Example

6 =1, 2, 3, 6 =(1+2+3) = 6
12 = 1, 2, 3, 6, 12 =(1+2+3+6) = 12
28 = 1, 2, 4, 7, 14, 28 =(1+2+4+7+14) = 28


import java.util.*;
class Perfect_No
{
    public static void main(String args[])
    {
        int s=0,i,n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number : ");
        n = sc.nextInt();    
        for(i=1; i<n; i++)
        {
            if(n%i==0)
            {
                s=s+i;
            }
        }
         
        if(s==n)
            System.out.println("Perfect Number");
        else
            System.out.println("Not a Perfect Number");      
    }
}
 

No comments

Powered by Blogger.