Perfect Number Program in Java

Write a Perfect Number Program in Java programming language using While Loop, For Loop, and Functions. We will also show the 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 is 1 + 2 + 4 + 7 + 14 = 28. Remember, we have to exclude the number itself. That is why we have not 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. Next, this program will check whether the number is Perfect number or not using the Java For Loop.

// 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);
		}
	}
}
Perfect Number Program in Java using For Loop

Within this Perfect Number in Java program, First, We declared three integer variables i, Number, and Sum = 0. The following statements will ask the user to enter any positive integer. Then, that number assigned to the 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 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.

  1. If the number is perfectly divisible by i, then i will be added to the Sum.
  2. If the number is not divisible by i, then i will be incremented by one and check for the 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, the 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, the program will start executing statements inside the For loop
  • Within the for loop we have If Else statement and the condition if (6 % 1 == 0) is TRUE so, Sum = Sum + i
  • Sum = 0 + 1 = 1
  • Lastly, i will be incremented by 1. Please refer to Increment and Decrement Operators in Java article to understand the ++ notation.

Second Iteration

From the second Iteration, the values of both Sum and i changed as Sum = 1 and i = 2

  • Condition inside the For loop (2 < 6) is TRUE.
  • Within the for loop, the if condition if (6 % 2 == 0) is TRUE. So, Sum = Sum + i
  • Sum = 1 + 2 = 3
  • Lastly, i value incremented by 1.

Third Iteration

From the third Iteration of Java perfect number program, the values of Sum = 3 and i = 3

  • Condition inside the For loop (3 < 6) is TRUE.
  • Within the for loop, the if condition if (6 % 3 == 0) is TRUE. So, Sum = Sum + i
  • Sum =3 + 3 = 6
  • Lastly, i value incremented by 1.

Fourth and Fifth Iterations

For the fourth and fifth iterations, the if condition will fail.

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

Sixth Iterations

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

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

if (Sum == Number)

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

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

If the condition (Sum == Number) is FALSE, below System.out.format statement 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. By using this number, this program will check whether the user input is a Perfect number or not using While Loop.

/* 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 have not done anything special in this example. We just replaced the For loop in Java perfect number program with While Loop. If you find it difficult to understand the While loop functionality, then please refer to Java While Loop article.

Java perfect number using a while loop output

Please Enter any Number: 
496
496 is a Perfect Number

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 a Perfect number or not using the Java For Loop

// 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);
		}
	}
}

Java perfect number using functions output

Please Enter any Number: 
28
28 is a Perfect Number

Here, our function declared as void, and it does not return any value. So, we are calling function.

PerfectNumber (Number);

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

public static void PerfectNumber (int Number) {

We already explained the LOGIC in the above example.

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 that holds a method to find the sum of factors.

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 class and call the methods

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

Please Enter any Number: 
8128
8128 is a Perfect Number

PerfectNumber Class Analysis:

Within this Java perfect number program, we created one function to find the factors of a given Number and summing those factors. We already explained the Logic in the 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 the user-specified number or not.

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

Perfect Number Program in Java using OOPS 2

From the above perfect number in java program, 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 the function parameter or not. Based on the result, System.out.format statement 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 the perfect number allows the user to enter the minimum and maximum values. This perfect number in java program will find the Perfect Number between the Minimum and Maximum values.

// 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);
			}
		}
	}
}

Java perfect numbers from 1 to 100 output

Please Enter the Minimum Value: 
1
Please Enter the Maximum Value: 
10000
6 	28 	496 	8128 	

Within this Java perfect number program, the following statements will ask the user to enter the minimum and maximum values. 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 the Javac to iterate between Minimum and Maximum Variables. Here, iteration starts at the Minimum, and it will not exceed the Maximum variable.

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

Inside the 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.

Comments are closed.