Perfect number program in Java

In this tutorial you will learn how to write a program in Java to check a given number is perfect or not.

Read This: What is Perfect Number? Write a program in C for Perfect Number.

What is Perfect Number?

A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. Proper divisors are the positive divisors of a number other than the number itself. In other words, a perfect number is a number whose divisors (excluding itself) add up to the number itself.

Here’s an example to illustrate a perfect number:

Example:

28 is Perfect number

  • The divisors of 28 are 1, 2, 4, 7, 14.
  • Excluding the number itself (28), the sum of its divisors is 1 + 2 + 4 + 7 + 14 = 28.
  • Therefore, 28 is a perfect number.

How this Java program will behave?

Java program for perfect number is written below. This program will take a positive number as an input and after performing some operation it will print given number is perfect or not.

Suppose you want to check a number like is perfect or not then you have to give that number as an input to this below program at time of execution.

After the calculation it will give you appropriate output.

Program 1: Perfect number program in Java

import java.util.*;  
class Main{
    public static void main(String ...args){
        int remainder, sum=0, i, originalNum;
        Scanner sc= new Scanner(System.in);
        System.out.print("Please enter a number: ");  
        originalNum = sc.nextInt();  
        for (i = 1; i <= originalNum/2; i++)
        {
            remainder = originalNum % i;
	        if (remainder == 0)
            {
                sum = sum + i;
            }
        }
        if (sum == originalNum)
            System.out.print("Given no. is perfect number");
        else
            System.out.print("Given no. is not a perfect number");
    }
}  

Output:

Please enter a number: 28
Given no. is perfect number

Program 2: Perfect Number Program Using Java 8

import java.util.*;  
import java.util.stream.IntStream;

class Main{
    public static void main(String ...args){
        int number = 28;

        if (number <= 1) {
            System.out.print("Given no. is perfect number");
        }

        int sum = IntStream.range(1, number)
                .filter(i -> number % i == 0)
                .sum();

        if(sum == number)
            System.out.print("Given no. is perfect number");
        else
            System.out.print("Given no. is not a perfect number");
    }
}  

Output

Given no. is perfect number

Conclusion

In this tutorial we have learnt the writing java Program to check the perfect numbers. We have seen both traditional java program and using java 8 stream api to check given number is perfect or not. A perfect number is defined as a positive integer whose proper divisors (excluding itself) sum up to the number itself.

The first program utilized traditional looping to calculate the sum of divisors for a user-input number. After finding the sum, compare with original number. If both are equal then printed “Given number is perfect number”, otherwise no.

In the second program we have using Java 8 approach. With the help of Stream API, we have generated a range of divisors and calculating their sum in a more functional style.

Both programs demonstrated successful execution with the example number 28 as 28 is perfect number. The use of traditional loops and the more modern Stream API showcased different programming styles, providing flexibility in choosing the approach that aligns with the developer’s preferences or project requirements.

Leave a Comment