Question 2 :

Write a program to accept a number and check whether the number is a Perfect Number or not

Explanation :

A perfect Number is a number such that the sum of its divisors(excluding the number itself) equals the number itself .
Example : 28
It's divisors are : 1 , 2 , 4 , 7 , 14 (28 won't be considered)
Sum : 28 Hence , the sum of divisors equals 28 . It's a perfect number .


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


           
                
Codely Prompt ----->java perfectNum.java
Enter a number
496
12121 is a perfect number 
                
            

Contribution by :-

Abhishek Image

Abhishek Raj