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
28 August, 2021

Implementing Stack using Array in Java
28 August, 2021

Constructor Programs
3 July, 2021