Perfect Number in Java
The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally, a Java application will be written to find every perfect integer inside the specified range. Java coding exams and educational courses usually inquire about the perfect number application.
What is Perfect Number in Java
A perfect number in java is a number in which all the factors of that number are combined or added, then the result would be the number itself. Mathematically, the sum of all the positive factors of a number except the number itself must be equal to the number.
Let’s see how it works with a small example
Consider a small number 6
The factors of 6 include 1,2,3 and the number 6 itself.
Now let’s sum up the factors except the number
1+2+3=6
As we can observe the above example the sum of 1,2,3 would be mathematically be equal to 6 and they are also the factors of 6.
Thus, it can be said that the number 6 is the perfect number.
Similarly, there exit many perfect numbers like 28, 496, 8128 and many others.
Now let us understand the algorithm or a method of finding the perfect numbers in general
How to find the perfect number
Step 1: Begin or read a value (a).
Step 2: Set another variable (m) to store summation.
Step 3: Using the loops find out all the factors of the given value (a).
Step 4: Compute the sum and store the sum value in (m).
Step 5: Now compare both the values (a) and (m).
- If the values are equal, then (a) is said to be the perfect number.
- Else the value (a) is not regarded as perfect number.
Ways to find a perfect number in Java
The below are the following techniques for finding the perfect number in java. They include
- By Method
- By While Loop
- By Recursion
By Method Technique
The below program depicts how to verify whether a number is perfect number or not by using the methods that perform the specified operation for verification.
File name: PerfNum.java
import java.util.Scanner; // Scanner helps in taking input dynamically
public class PerfNum // name of the class
{
static long perfect(long n) // name of the method
{
long s=0; // Initially setting the sum as zero
for(int i=1; i <= n/2; i++)
{
if(n % i == 0)
{
s=s+ i; // storing the sum of factors
}
}
return s; //returning the sum
} // End of the method
public static void main(String args[])
{
long num, m;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
num=sc.nextLong(); // Takes a number as input from the user
m = perfect(num); // calling the method
if(m==num) // comparing the m and num
System.out.println(num+" is a perfect number");
else
System.out.println(num+" is not a perfect number");
}
}
Output
Enter the number: 28
28 is a perfect number
Enter the number: 7
7 is not a perfect number
By While Loop Technique
The below program depicts how to verify whether a number is perfect number or not by using the while loop that helps in performing the specified operation for verification.
File name: PerfNum1.java
import java.util.Scanner; // Enables in taking the input dynamically
public class PerfNum1 // name of the class
{
public static void main(String args[])
{
long n, s=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
n=sc.nextLong(); // Taking a number as input from user
int i=1;
while(i <= n/2) // Begin of while loop
{
if(n % i == 0)
{
s= s+ i;
}
i++; // Incrementing the value
} // End of While
if(s==n) //Comparing the n and s
{
System.out.println(n+" is a perfect number.");
}
else
System.out.println(n+" is not a perfect number.");
}
}
Output
Enter the number: 15
15 is not a perfect number.
Enter the number: 496
496 is a perfect number.
By using Recursion method
The below program depicts how to verify whether a number is perfect number or not by using the recursion technique that helps in performing the specified operation for verification.
File name: PerfNum2.java
import java.util.Scanner;
public class PerfNum2
{
static long s=0;
long perfect(long n, int i)
{
if(i <= n/2)
{
if(n% i ==0)
{
s=s + i;
}
i++;
perfect(n, i);
}
return s;
}
public static void main(String args[])
{
long num, m;
int i=1;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
num=sc.nextLong();
PerfNum2 p=new PerfNum2( );
m = p.perfect(num, i);
if(m == num)
System.out.println(num+" is a perfect number");
else
System.out.println(num+" is not a perfect number");
}
}
Output
Enter the number: 25
25 is not a perfect number
Enter the number: 6
6 is a perfect number