# How to implement Perfect Number in Java?

Last updated on Jun 17,2021 13.2K Views

11 / 29 Blog from Java Programs

A number is said to be a perfect number if the sum of its positive divisors excluding the number itself is equal to that number. Sounds interesting, isn’t it? In this article, let’s check out how to implement a perfect number in java.

Let’s begin!

## What is a Perfect Number in Java?

In simple terms, perfect number is equal to the sum of its proper divisors excluding the number itself. Let me cite an example to help you understand it better. Let’s consider a few examples:
Example1: 6
Positive factors are; 1,2,3,6
Here, the sum of all the factors excluding the number itself is equal to 6.
Example2: 28
Positive factors are; 1,2,4,7,14,28
Again the sum of all the factors excluding the number itself comes out to be 28.

Now that you are clear with the meaning of a perfect number let us move towards our next segment.

## Time complexity

The time complexity of a perfect number is √n.

Let us now see the implementation process of a perfect number in Java.

## Java Program to Implement a Perfect Number

Code:

```import java.util.Scanner;
public class Perfect
{
public static void main(String[] args)
{
int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter an integer:");
n = s.nextInt();
for(int i = 1; i &lt; n; i++)
{
if(n % i == 0)
{
sum = sum + i;
}
}
if(sum == n)
{
System.out.println("The number is Perfect");
}
else
{
System.out.println("The number is not Perfect");
}
}
int divisor(int x)
{
return x;
}
}

```

Output:

```Enter an integer: 46
The number is not Perfect
```

This brings us to the end of this ‘Perfect Number in Java’ article. I have covered one of the most fundamental and important topics of Java. Hope you are clear with all that has been shared with you in this article.

Make sure you practice as much as possible and revert your experience.

Check out the Java Course Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.

Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 3rd August,2024

3rd August

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 28th September,2024

28th September

SAT&SUN (Weekend Batch)
View Details