# Find Perfect Number in Java

In this article, we are going to find whether a number is `perfect` or not using Java.

A number is called a `perfect number` if the sum of its divisors is equal to the number. The sum of divisors excludes the number. There may be several approaches to find the perfect number.
For example:

28 is perfect number:
– 28’s divisors are 1, 2, 4, 7, 14
– sum of divisors of 28 excluding itself is 28.

Here, we are using two main approaches iterative and recursive approach.

Let’s see the example.

## Iterative approach

In this example, we are using the while loop to find divisors of the number and then get sum of them.
Here are the steps:

1. Initiaze `sum=0` and loop variable `i=1`.
2. Iterate a while loop until condition `i<=number/2` is false.
3. Check remainder of `number%i` using modulo operator. If remainder is 0, then add it to the `sum`.
4. If `sum` is equals to number after all iterations, then it is `perfect number`.

Output

It is Perfect Number

## Recursive approach

We can use recursion technique, if don’t want to use while loop. It reduces code statements but provides the accurate result.
Here are the steps:

1. Initialize static variable `sum=0`.
2. Create a function `findPerfect()` which will take two parameters `number` and `div`
3. Check if `div<=number/2` is true. This will be the terminal condition for recursion.
1. Check remainder of `number%i` using modulo operator. If remainder is 0, then add it to the `sum`.
2. Increment `div`
3. Call `findPerfect()` recursively.
4. If returned output of `findPerfect()` is equals to number, then it is `perfect number`.
See the example below.

Output

It is Perfect Number

That’s all about how to find Perfect Number in Java.

import_contacts

### You may also like: ## Related Posts

• 05 April

### Count occurrences of Character in String in Java

Table of Contents1. Using String Library Methods2. Using Recursion3. Using Hashing ConceptUsing ArraysUsing Collections (Map) 4. Using Java 8 Features In this article, we will look at a problem: Given an Input String and a Character, we have to Count Occurrences Of character in String in java. For Example, If the Given String is : […]

• 24 December

### How to capitalize first letter in java

Learn about how to capitalize first letter in java.

• 09 October

### Java program to find first and last digit of a number

Table of ContentsAlgorithmUsing while loopUsing log() and pow() methodsUsing while loop and pow() method In this article, we are going to find first and last digit of a number in Java. To find first and last digit of any number, we can have several ways like using modulo operator or pow() and log() methods of […]

• 03 October

### Happy Number program in Java

Table of ContentsWhat is a Happy Number?Using HashsetAlgorithmExampleUsing slow and fast pointersAlgorithmExample In this article, we are going to learn to find Happy Number using Java. Let’s first understand, what is Happy Number? What is a Happy Number? A number which leaves 1 as a result after a sequence of steps and in each step […]

• 28 September

### How to find Magic Number in Java

Table of ContentsWhat is a Magic Number?Algorithm for Magic NumberExample to find Magic NumberAnother Example To find Magic Number In this article, we are going to learn to find Magic Number using Java. Let’s first understand, what is Magic Number? What is a Magic Number? A number which leaves 1 as a result after a […]

• 28 September

### Number guessing game in java

Table of ContentsNumber guessing game RulesAlgorithm for Number guessing game In this article, we will implement Number guessing game in java. The number guessing game is based on a concept where player guesses a number between a range. If player guesses the exact number then player wins else player looses the game. Since this game […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.