![Perfect number](https://simplycoding.in/wp-content/uploads/2020/04/Perfect-number-1.jpg)
Java Program on Perfect Number
- Categories Java
Accept a number from user and print if given number is Perfect number or not.
Perfect numbers : the sum of its positive divisors excluding the number itself is equal to that number.
For example, 28 is a perfect number because, 28 is divisible by 1, 2, 4, 7, 14 and 28 ,and the sum of these values is 1 + 2 + 4 + 7 + 14 = 28.
import java.util.Scanner; public class PerfectNumber { public static void main(String[] args) { int n, Sum = 0 ; Scanner sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); n = sc.nextInt();
for(int i = 1; i < n; i++) {
if (n % i == 0)
sum = sum + i;
}
if (sum == n)
System.out.println("Yes");
else
System.out.println("No");
} }
You may also like
![Single Linked List Programs in Java Single Linked List Programs in Java](https://simplycoding.in/wp-content/uploads/2021/07/Single-Linked-List-Programs-in-Java-300x200.png)
Single Linked List Programs in Java
28 August, 2021
![Implementing Stack using Array in Java Implementing Stack using Array in Java](https://simplycoding.in/wp-content/uploads/2021/08/Implementing-Stack-using-Array-in-Java-300x200.png)
Implementing Stack using Array in Java
28 August, 2021
![Constructor Programs Constructor Programs](https://simplycoding.in/wp-content/uploads/2021/06/Constructor-Programs-300x200.jpg)
Constructor Programs
3 July, 2021