Perfect Number program in Java.

Home / Uncategorized / Perfect Number program in Java.

Perfect Number program in Java.

What is Perfect Number?

 

Perfect number, a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128.

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

Output:

Enter your Number:6
your number 6 is a perfect number.

Watch video of this program

Leave a Reply

Your email address will not be published. Required fields are marked *