Java Program to Check If a Number is a Perfect Number or Not

Perfect numbers

Java programs to check if a number is a perfect number or not have been shown here. A perfect number is a positive integer that is equal to the sum of its proper divisors. For example 6 is a perfect number because if we add it's proper divisors (1, 2 and 3), we get 6 i.e. 1 + 2 + 3 = 6. Some of the other perfect numbers are 28, 496, 8128 etc.






1. Algorithm to check if a number is a perfect number or not


1. Take a number n as input.

2. Set s = 0, x = 1

3. Check if n is divisible by x

4. If step 3 is true perform s = s + x

5. Set x = x + 1

6. Go step 3 until x > n/2

7. If s = n declare "n is a perfect number"




2. Pseudocode to check if a number is a perfect number or not


Input : A positive number n

Output : n is a perfect number or not

1. Procedure isPerfectNumber(n):

2. s := 0

3. x := 1

4. Repeat until x > (n/2)

5. If n mod x = 0:

6. s := s + x

7. If n = s:

8. Return "Perfect number"

9. Else:

10. Return "Not a perfect number"

11. End Procedure





3. Time complexity to check if a number is a perfect number or not


Time Complexity: O(n)

n is the input number




4. Program & Output to check if a number is a perfect number or not




4.1. Java Program & Output to check if a number is a perfect number or not using iteration

Code has been copied
/**************************
     alphabetacoder.com
Java program to check if a
number is a perfect number
***************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int n, i, sum = 0;

        // take inputs
        System.out.print("Enter the number: ");
        n = sc.nextInt();

        // add all the divisor upto n / 2
        for (i = 1; i <= (n / 2); i++) {
            if (n % i == 0)
                sum = sum + i;
        }

        // check if the input number 
        // is equal to the sum
        if (n == sum)
            System.out.print(n + " is a perfect number");
        else
            System.out.print(n + " is not a perfect number");
    }
}

Output


Enter the number: 28

28 is a perfect number




4.2. Java Program & Output to check if a number is a perfect number or not using recursion

Code has been copied
/************************************
	  alphabetacoder.com
Java program to check if a number is 
a perfect number using recursion
*************************************/

import java.util.Scanner;

class Main {
    // recursive function to check if input 
    // number is a perfect number or not
    public int isPerfect(int n, int i, int sum) {
        if (i > n / 2) // exit condition
            return (sum == n) ? 1 : 0;

        if (n % i == 0) // check divisibility
            sum += i;

        return isPerfect(n, i + 1, sum);
    }
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare object of class Main
        Main obj = new Main();

        // declare variables
        int n;

        // take inputs
        System.out.print("Enter the number: ");
        n = sc.nextInt();

        // check if the input number is perfect
        // by calling function
        if (obj.isPerfect(n, 1, 0) == 1)
            System.out.print(n + " is a perfect number");
        else
            System.out.print(n + " is not a perfect number");
    }
}

Output


Enter the number: 500

500 is not a perfect number