Write a Perfect Number Program in Java programming language using While Loop, For Loop and Functions. We will also show you, Java Perfect Numbers between 1 to n.

## Perfect Number in Java

Any number can be a Java Perfect Number, If the sum of its positive divisors excluding the number itself is equal to that number. For example, 28 is a perfect number because 28 is divisible by 1, 2, 4, 7, 14 and 28 and the sum of these values are: 1 + 2 + 4 + 7 + 14 = 28 (Remember, we have to exclude the number itself. That’s why we haven’t added 28 here). Some of the Java perfect numbers are 6, 28, 496, 8128 and 33550336 so on.

## Perfect Number Program in Java using For Loop

This Java program for Perfect Number allows the user to enter any number. Using this number, this program will check whether the number is Perfect number or not using the Java For Loop

**JAVA CODE**

// Write Perfect Number program in Java using For Loop */ package FrequentPrograms; import java.util.Scanner; public class PerfectNumberUsingFor { private static Scanner sc; public static void main(String[] args) { int i, Number, Sum = 0 ; sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); Number = sc.nextInt(); for(i = 1 ; i < Number ; i++) { if(Number % i == 0) { Sum = Sum + i; } } if (Sum == Number) { System.out.format("\n% d is a Perfect Number", Number); } else { System.out.format("\n% d is NOT a Perfect Number", Number); } } }

**OUTPUT**

**ANALYSIS**

Within this Perfect Number in Java program, First, We declared three integer variables i, Number and Sum = 0. Following statements will ask the user to enter any positive integer and then, that number is assigned to variable Number.

System.out.println("\n Please Enter any Number: "); Number = sc.nextInt();

In the next line we have For Loop and the condition inside the for loop (i < Number) will make sure that i value should not exceed the number. Within the For loop we used If Condition to check whether the Number is Perfectly divisible by i value or not.

- If the Number is perfectly divisible by i then i value will be added to the Sum
- If the Number is not divisible by i then i value will be incremented by 1 and check for next value

Please refer Java If Else statement article to understand the If else statement

for(i = 1 ; i < Number ; i++) { if(Number % i == 0) { Sum = Sum + i; } }

Let us see the working principle of this for loop in iteration wise. From the above Java perfect number example screenshot you can observe that, User entered value: Number = 6

**First Iteration**

For the first Iteration, Number = 6, Sum = 0 and i = 1

- Condition inside the For loop (1 < 6) is TRUE so, program will start executing statements inside the For loop
- Within the for loop we have If Else statement and the condition
is TRUE so, Sum = Sum + i**if (6 % 1 == 0)** - Sum = 0 + 1 = 1
- Lastly, i value will be incremented by 1. Please refer Increment and Decrement Operators in Java article to understand the ++ notation.

**Second Iteration**

From the second Iteration the values of both Sum and i has been changed as: Sum = 1 and i = 2

- Condition inside the For loop (2 < 6) is TRUE so, program will start executing statements inside the For loop
- Within the for loop, the if condition
is TRUE so, Sum = Sum + i**if (6 % 2 == 0)** - Sum = 1 + 2 = 3
- Lastly, i value will be incremented by 1.

**Third Iteration**

From the third Iteration of Java perfect number program, the values of both Sum and i has been changed as: Sum = 3 and i = 3

- Condition inside the For loop (3 < 6) is TRUE so, program will start executing statements inside the For loop
- Within the for loop, the if condition
is TRUE so, Sum = Sum + i**if (6 % 3 == 0)** - Sum =3 + 3 = 6
- Lastly, i value will be incremented by 1.

**Fourth and Fifth Iterations**

For the fourth and fifth iterations condition inside the if condition will fail

is FALSE**if (6 % 4 == 0)**is FALSE**if (6 % 5 == 0)**

**Sixth Iterations**

For the sixth iteration, value of i become 6 which means the condition inside the for loop will fail (6 < 6). So, compiler will terminate the for loop.

In the next line we have If condition to check whether the value inside the Sum variable is exactly equal to given Number or Not.

if (Sum == Number)

If the condition (Sum == Number) is TRUE, below **System.out.format **statement will be executed

System.out.format("\n% d is a Perfect Number", Number);

If the condition (Sum == Number) is FALSE, below **System.out.format **statement will be executed

System.out.format("\n% d is NOT a Perfect Number", Number);

For this example (Sum == Number) is True. So, given Number is Perfect Number

## Perfect Number Program in Java using While Loop

This perfect number in Java program allows the user to enter any number. Using this number, this program will check whether the user input is Perfect number or not using While Loop

**JAVA CODE**

/* Write Perfect Number program in Java using While Loop */ package FrequentPrograms; import java.util.Scanner; public class PerfectNumberUsingWhile { private static Scanner sc; public static void main(String[] args) { int i = 1, Number, Sum = 0 ; sc = new Scanner(System.in); System.out.println("Please Enter any Number: "); Number = sc.nextInt(); while(i < Number) { if(Number % i == 0) { Sum = Sum + i; } i++; } if (Sum == Number) { System.out.format("%d is a Perfect Number", Number); } else { System.out.format("%d is NOT a Perfect Number", Number); } } }

We haven’t done anything special in this example. We just replaced the For loop in Java perfect number program with While Loop. If you find difficult to understand the While loop functionality then please refer Java While Loop article.

**OUTPUT**

## Perfect Number Program in Java using Functions

This Java perfect number program allows the user to enter any integer value and we are going to pass the User entered value to the Method we created.

Within this User defined function, this perfect number in java program will check whether the user input is Perfect number or not using the Java For Loop

**JAVA CODE**

// Perfect Number Program in Java using Functions package FrequentPrograms; import java.util.Scanner; public class PerfectNumberUsingMethod { private static Scanner sc; public static void main(String[] args) { int Number; sc = new Scanner(System.in); System.out.println("Please Enter any Number: "); Number = sc.nextInt(); PerfectNumber (Number); } public static void PerfectNumber (int Number) { int i, Sum = 0; for(i = 1 ; i < Number ; i++) { if(Number % i == 0) { Sum = Sum + i; } } if (Sum == Number) { System.out.format("%d is a Perfect Number", Number); } else { System.out.format("%d is NOT a Perfect Number", Number); } } }

**OUTPUT**

**ANALYSIS**

Here our function is declared as void and it does not return any value so, we are calling function as we shown below

PerfectNumber (Number);

When the compiler reaches to PerfectNumber(Number) line in main() program, the compiler will immediately jump to below function:

public static void PerfectNumber (int Number) {

We already explained the LOGIC in above example.

**NOTE: **If we create a function with Void then there is no need to return any value but, if we declared a function with any data type (int, float etc) then we have return something out from the function.

## Perfect Number Program in Java using OOPS

In this Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a method to find the sum of factors.

**JAVA CODE**

package FrequentPrograms; public class PerfectNumber { int i, Sum = 0; public int FindPerfectNumber (int Number) { for(i = 1; i < Number; i++) { if(Number % i == 0) { Sum = Sum + i; } } return Sum; } }

Within the Main program of a Java perfect number, we will create an instance of the above specified class and call the methods

**JAVA CODE**

package FrequentPrograms; import java.util.Scanner; public class PerfectNumberUsingClass { private static Scanner sc; public static void main(String[] args) { int Number, Sum = 0; sc = new Scanner(System.in); System.out.println("Please Enter any Number: "); Number = sc.nextInt(); PerfectNumber pn = new PerfectNumber(); Sum = pn.FindPerfectNumber(Number); if (Sum == Number) { System.out.format("%d is a Perfect Number", Number); } else { System.out.format("%d is NOT a Perfect Number", Number); } } }

**OUTPUT**

**ANALYSIS**

**PerfectNumber Class Analysis:**

Within this Java perfect number program, First, we created one function to find the factors of a given Number and summing those factors. We already explained the Logic in above example.

**Main Class Analysis:**

First we created an instance / created an Object of the * PerfectNumber* Class

PerfectNumber pn = new PerfectNumber();

Next, we are calling the *FindPerfectNumber(Number)* method. As we all know that the method will return integer value so, we are assigning that return value to Sum

Sum = pn.FindPerfectNumber(Number);

Lastly, we used the Java If Else statement to check the return value is equal to user specified number or not.

We can also create one more method inside our * PerfectNumber* Class to check whether Sum is equal to number or not

From the above perfect number in java program you can observer that, First we created one function to find the factors of a given Number and summing those factors. Next, we declared one more function to check whether the first function return value is equal to function parameter or not. Based on the result, * System.out.format* statement will be executed.

**NOTE:** With the PerfectNumberAgain(int Num) method we are calling the FindPerfectNumber(Num) function to get the sum of the factors.

## Java Program to Find Perfect Number between 1 to 1000

This Java program for perfect number allows the user to enter minimum and maximum values. This perfect number in java program will find the Perfect Number between the Minimum and Maximum values.

**JAVA CODE**

// Java Program to Find Perfect Number between 1 to 1000 package FrequentPrograms; import java.util.Scanner; public class PerfectNumbersbetweenMin { private static Scanner sc; public static void main(String[] args) { int i, Number, Minimum, Maximum, Sum = 0; sc = new Scanner(System.in); System.out.println("Please Enter the Minimum Value: "); Minimum = sc.nextInt(); System.out.println("Please Enter the Maximum Value: "); Maximum = sc.nextInt(); for(Number = Minimum; Number <= Maximum; Number++) { for(i = 1, Sum =0; i < Number; i++) { if(Number % i == 0) { Sum = Sum + i; } } if (Sum == Number) { System.out.format("%d \t", Number); } } } }

**OUTPUT**

**ANALYSIS**

Within this Java perfect number program, the following statements will ask the user to enter minimum and maximum values and, then we are going to assign user entered values to Minimum and Maximum variables

System.out.println("Please Enter the Minimum Value: "); Minimum = sc.nextInt(); System.out.println("Please Enter the Maximum Value: "); Maximum = sc.nextInt();

Next we used the For Loop and this Loop helps compiler to iterate between Minimum and Maximum Variables. Here, iteration starts at the Minimum and it will not exceed Maximum variable.

for(Number = Minimum; Number <= Maximum; Number++) {

Inside the Java perfect number program for loop we are checking whether the number is perfect number or not. We already explained the for loop iteration in the first example.