Perfect Number in Java
What is a Perfect Number?
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, and 1 + 2 + 3 = 6, so 6 is a Perfect Number.
Compile and Run the following code with Command-Prompt Or download Notepad++
For Compilation javac Sample.java
For Execution java Sample
import java.util.Scanner; class Sample { public static void main(String args[]) { int a,i = 1,sum = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter a number of your choice: "); a = sc.nextInt(); while(i < a) { if(a%i == 0) { sum = sum + i; } i++; } if(sum == a) System.out.println("It is a perfect number"); else System.out.println("It is not a perfect number"); } }