Dark Mode On/Off

# Java Program To Find The Perfect Number

A number is called a perfect number if the sum of all its factors excluding the number itself is equal to the number. For example, consider the number 6. The factors of 6 are 1,2,3 and 6. Now, the sum of all its factors excluding the number itself is 1+2+3=6.

Here, since the original number is equal to the sum of all its factors excluding the number itself, therefore it is a perfect number.

In this tutorial, we will learn how to find the perfect number in java. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java

Input: Enter the number: 34

Output: 34 is not a perfect number

## Java Program to find the Perfect Number

In this program, we will check whether the number is perfect or not using a for loop.

### Algorithm

1. Start

2. Create an instance of the Scanner class.

3. Declare a variable.

4. Ask the user to initialize the variable.

5. Call a method to check whether the number is perfect or not.

6. Declare a variable to store the sum of divisors.

7. Initialize the sum to 1.

8. Use a for loop to find the divisors of the entered number.

9. Update the sum each time a divisor of the entered number encounters.

10. If the sum of all the divisors of the entered numbers is equal to the entered number, then print it as a perfect number.

11. If the sum of all the divisors of the entered numbers is not equal to the entered number, then print it as not a perfect number.

12. Display the result.

13. Stop

Below is the Java code to find the perfect number.

``````// Program to find the perfect number in Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = sc.nextInt();
if (checkPerfect(num))
{
System.out.print(num+" is a perfect number ");
}
else
{
System.out.println(num+" is not a perfect number ");
}

}
static boolean checkPerfect(int num)
{
// To store sum of divisors
int sum = 1;

// Find all divisors and add them
for (int i = 2; i * i <= num; i++)
{
if (num % i==0)
{
if(i * i != num)
sum = sum + i + num / i;
else
sum = sum + i;
}
}
// If sum of divisors is equal to number
// Then number is a perfect number
if (sum == num && num != 1)
return true;

return false;
}
} ``````

Enter the number: 28
28 is a perfect number

## Program 2: Java Program to find the Perfect Number

In this program, we will check whether the number is perfect or not using a while loop.

### Algorithm

1. Start

2. Create an instance of the Scanner class.

3. Declare a variable.

4. Ask the user to initialize the variable.

5. Call a method to check whether the number is perfect or not.

6. Declare a variable to store the sum of divisors and another loop variable.

7. Initialize the sum to 0 and the loop variable to 1.

8. Use a while loop to find the divisors of the entered number.

9. Update the sum each time a divisor of the entered number encounters.

10. Increment the loop variable after each iteration.

11. If the sum of all the divisors of the entered numbers is equal to the entered number, then print it as a perfect number.

12. If the sum of all the divisors of the entered numbers is not equal to the entered number, then print it as not a perfect number.

13. Display the result.

14. Stop

Below is the Java code to find the perfect number.

``````// Program to find the perfect number in Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = sc.nextInt();
checkPerfect(num);    //Call a method to check perfect

}
//Checks and Prints whether perfect or not
static void checkPerfect(int num)
{
// To store sum of divisors
int sum = 0,i=1;
while(i<num)
{
if(num % i == 0)
{
sum = sum + i;
}
i++;
}
if(sum == num)
{
System.out.println("The entered number "+num+" is a Perfect number");
}
else
{
System.out.println("The entered number "+num+" is not a Perfect number");
}
}
}
``````

Enter the number: 35
The entered number 35 is not a Perfect number

## Program 3: Java Program to find the Perfect Number

In this program, we will find the perfect numbers within a range.

### Algorithm

1. Start

2. Create an instance of the Scanner class.

3. Declare the range.

4. Ask the user to initialize the range.

5. Use a for loop to iterate over the elements within that range.

6. Call a method to check whether the number is perfect or not.

7. Declare a variable to store the sum of divisors and another loop variable.

8. Initialize the sum to 0 and the loop variable to 1.

9. Use a while loop to find the divisors of the entered number.

10. Update the sum each time a divisor of the entered number encounters.

11. Increment the loop variable after each iteration.

12. If the sum of all the divisors of the entered numbers is equal to the entered number, then return true.

13. If the sum of all the divisors of the entered numbers is not equal to the entered number, then return false.

14. Display the perfect elements.

15. Stop

Below is the Java code to find the perfect number.

``````// Program to find the perfect number in Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int num1 = sc.nextInt();
System.out.println("Enter the second number: ");
int num2 = sc.nextInt();
System.out.println("Perfect numbers within the given range are: ");
for(int i=num1;i<=num2;i++)
{
if(checkPerfect(i))
System.out.print(i+" ");
}
}
//Checks and Prints whether perfect or not
static boolean checkPerfect(int num)
{
// To store sum of divisors
int sum = 0,i=1;
while(i<num)
{
if(num % i == 0)
{
sum = sum + i;
}
i++;
}
if(sum == num)
return true;

return false;
}
}
``````

Enter the first number: 2
Enter the second number: 50
Perfect numbers within the given range are:
6 28