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
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable.
-
Ask the user to initialize the variable.
-
Call a method to check whether the number is perfect or not.
-
Declare a variable to store the sum of divisors.
-
Initialize the sum to 1.
-
Use a for loop to find the divisors of the entered number.
-
Update the sum each time a divisor of the entered number encounters.
-
If the sum of all the divisors of the entered numbers is equal to the entered number, then print it as a perfect number.
-
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.
-
Display the result.
-
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
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable.
-
Ask the user to initialize the variable.
-
Call a method to check whether the number is perfect or not.
-
Declare a variable to store the sum of divisors and another loop variable.
-
Initialize the sum to 0 and the loop variable to 1.
-
Use a while loop to find the divisors of the entered number.
-
Update the sum each time a divisor of the entered number encounters.
-
Increment the loop variable after each iteration.
-
If the sum of all the divisors of the entered numbers is equal to the entered number, then print it as a perfect number.
-
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.
-
Display the result.
-
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
-
Start
-
Create an instance of the Scanner class.
-
Declare the range.
-
Ask the user to initialize the range.
-
Use a for loop to iterate over the elements within that range.
-
Call a method to check whether the number is perfect or not.
-
Declare a variable to store the sum of divisors and another loop variable.
-
Initialize the sum to 0 and the loop variable to 1.
-
Use a while loop to find the divisors of the entered number.
-
Update the sum each time a divisor of the entered number encounters.
-
Increment the loop variable after each iteration.
-
If the sum of all the divisors of the entered numbers is equal to the entered number, then return true.
-
If the sum of all the divisors of the entered numbers is not equal to the entered number, then return false.
-
Display the perfect elements.
-
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