Java Program to Find Perfect Number

In this post, we will learn to code the Java Program to Find Perfect Number. Let’s understand about Perfect number and How to Find Perfect Number in Java Programming Language.

What is Perfect Number ?

In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number.

Let’s see the Java Program to Find Perfect Number.

Java Program to Find Perfect Number

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int num = sc.nextInt();
        
        int sum = 0;
        
        // Calculating sum of all positive divisors
        for (int i = 1; i <= num/2; i++){
            if (num % i == 0){
                sum = sum + i;
            }
        }
        
        // Checking whether sum is equal to original number
        if (num == sum){
            System.out.println(""+num+" is a perfect number.");
        }
        else{
            System.out.println(""+num+" is NOT a perfect number.");
        }

    }
    
}

Output 

Enter a number: 6
6 is a perfect number.

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int num = sc.nextInt();

In this Program, first, we take the input of the number from the user using the Scanner Class in Java.

        int sum = 0;
        
        // Calculating sum of all positive divisors
        for (int i = 1; i <= num/2; i++){
            if (num % i == 0){
                sum = sum + i;
            }
        }

Then, We use a for loop to calculate the sum of the divisors.

1st For Loop Iteration: (i = 1; i <= 6/2; i++)
If (6 % 1 == 0), here the condition is true, so sum = sum + i = 0 + 1 = 1.

2nd For Loop Iteration: (i = 2; i <= 6/2; i++)
If (6 % 2 == 0), here the condition is true, so sum = sum + i = 1 + 2 = 3.

3rd For Loop Iteration: (i = 3; i <= 6/2; i++)
If (6 % 3 == 0), here the condition is true, so sum = sum + i = 3 + 3 = 6.

        // Checking whether sum is equal to original number
        if (num == sum){
            System.out.println(""+num+" is a perfect number.");
        }
        else{
            System.out.println(""+num+" is NOT a perfect number.");
        }

Now, we check whether the sum is equal to the original number or not. If the sum is equal to num, then the entered number is a perfect number otherwise the entered number is not a perfect number.

This is the Java Program to Find Perfect Number.

Read more: Java Program to Check Armstrong Number

Conclusion

I hope after going through this post, you understand Java Program to Find Perfect Number.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *