# Perfect Number Program in Java Example.

by Dinesh Thakur Category: Control Structures

A Perfect Number is sum of its positive divisors, excluding the number itself. The First Perfect number is 6, For example 6 is a perfect number as 1,2 and3 are its divisors and the sum of divisors= ( 1 + 2 + 3)  = 6.

Algorithm for Perfect Number Program:

step 2: Set b=1, s=0

step 3: Repeat through step-5 until (b < num)

step 4: If (num mod b) equals to 0 then s=s+b

step 5: b=b+1

step 6: If s equals to num then print “num is perfect”

Else print “num is not perfect”

Step 7: Exit

Here is the Java Example for Perfect Number Program:

```import java.util.Scanner;

class PerfectNumber

{

public static void main(String args[])

{

Scanner sl=new Scanner(System.in);

System.out.println("Enter A Number");

int num=sl.nextInt();

int b=1, s=0;

while(b<num )

{

if(num%b == 0)

{

s += b;

}

b++;

}

if(s == num)

{

System.out.println(num+" is a perfect number");

}

else

{

System.out.println(num+" is not a perfect number");

}

}

}

```

Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles