Java Program to check a number is perfect number or not


In this program, You will learn how to check a number is perfect number or not in Java.


Some list of perfect numbers is : 6, 28, 496, 8128


Example: How to check a number is perfect number or not in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int n, i, s = 0;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a Number :");
        n = sc.nextInt();

        for (i = 1; i < n; i++) {
            if (n % i == 0)
                s = s + i;
        }
        if (s == n){
          System.out.println("\nIt is a Perfect Number :" + n);
        }else{
          System.out.println("\nIt is Not a Perfect Number :" + n);
        }

    }
}

Output:

Enter a Number :28
It is a Perfect Number :28