Perfect number in Java
In this post, you will learn how to find the perfect number using the Java programming language. The perfect number program is generally asked in Java coding tests and interviews.
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. In other words, a number whose sum of factors is equal to the number, excluding the number itself is called a perfect number.
The sum of divisors of a number, excluding the number itself, is called its aliquot sum, so a perfect number is one that is equal to its aliquot sum. For example, 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number. 6 is the first perfect number.
Perfect Number | Sum of its Divisors |
6 | 1+2+3 |
28 | 1+2+4+7+14 |
496 | 1+2+4+8+16+31+62+124+248 |
8,128 | 1+2+4+8+16+32+64+127+254+508+1,016+2,032+4,064 |
Here, we have mentioned three different ways to find the perfect number in Java.
Perfect Number Program in Java using For loop
The given Java program ask the user to enter any number. Next, this will check whether the number is Perfect number or not using the for loop.
// Perfect Number program in Java using For Loop */
import java.util.Scanner;
public class PerfectNumberProgram {
private static Scanner sc;
public static void main(String[] args) {
int i, num, sum = 0 ;
sc = new Scanner(System.in);
System.out.println("\n Please Enter Number: ");
num = sc.nextInt();
for(i = 1 ; i < num ; i++) {
if(num % i == 0) {
sum = sum + i;
}
}
if (sum == num) {
System.out.format("\n%d is a Perfect Number.", num);
}
else {
System.out.format("\n%d is NOT a Perfect Number.", num);
}
}
}
Output of the above code:
Please Enter Number:
48
48 is NOT a Perfect Number.
Please Enter Number: 6
6 is a Perfect Number.
Perfect Number Program in Java using While loop
In the given Java program, we asked the user to enter the number. Then, we have used the while loop to check whether the number is perfect or not.
// Perfect Number program in Java using While Loop */
import java.util.Scanner;
public class PerfectNumberProgram {
public static void main(String args[])
{
long num, sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
num=sc.nextLong();
int i=1;
while(i <= num/2)
{
if(num % i == 0)
{
//calculating the sum of factors
sum = sum + i;
}
i++;
}
//comparing the sum with the number
if(sum==num)
{
System.out.println(num+" is a perfect number.");
}
else
System.out.println(num+" is not a perfect number.");
}
}
Output of the above code:
Enter the number: 6
6 is a perfect number.
Enter the number: 496
496 is a perfect number.
Perfect Number Program in Java using Method
Here, we have asked the user to enter any integer value and defined a method checkPerfect(). We have passed the user entered value to the method we created. Within the user defined function, we have used the for loop to check whether the user input is a perfect number or not.
// Perfect Number program in Java using For Loop */
import java.util.Scanner;
public class CheckPerfectNumber
{
//function to check the perfect number
static long checkPerfect(long num)
{
long sum=0;
//executes until the condition becomes false
for(int i=1; i <= num/2; i++)
{
if(num % i == 0)
{
//calculating the sum of factors
sum=sum + i;
}
} return sum;
} //end of method
// Driver code
public static void main(String args[])
{
long number, sum;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
//reads a number from the user
number=sc.nextLong();
//calling the function
sum = checkPerfect(number);
//compares sum with the number
if(sum==number) {
System.out.println(number+" is a perfect number.");
}
else {
System.out.println(number+" is not a perfect number.");
}
}
}
Output of the above code:
Enter the number: 6
6 is a perfect number.
Enter the number: 8128
8128 is a perfect number.
Perfect Number Program in Java using Recursion
Here is the Java program to find the perfect number using the recursion function. Recursion function is a function which is called by itself.
import java.util.Scanner;
public class CheckPerfectNumber
{
static long sum=0;
long checkPerfect(long num, int i)
{
if(i <= num/2)
{
if(num % i ==0)
{
//calculating the sum of factors
sum=sum + i;
}
i++;
// calling function recursively
checkPerfect(num, i);
}
//returns the sum of factors
return sum;
}
//Driver code
public static void main(String args[])
{
long number, s;
int i=1;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
//input from the user
number=sc.nextLong();
//creating an object of the class
CheckPerfectNumber obj=new CheckPerfectNumber( );
s = obj.checkPerfect(number, i);
//compares sum with the number
if(s == number)
System.out.println(number+" is a perfect number.");
else
System.out.println(number+" is not a perfect number.");
}
}
Output of the above code:
Enter the number: 28
28 is a perfect number.
Perfect Number Program in Java between given range
In the given Java program, we asked the user to enter the start and end numbers. Then, we have used the for loop to check whether the number is perfect or not between the given range.
import java.util.Scanner;
public class PerfectNumberProgram
{
//checks if the given number is perfect or not
static boolean chkPerfectNumber(long num)
{
long sum = 1;
// find out all the divisors of the given number
// 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
//calculates the sum of factors
sum = sum + i;
}
}
if (sum == num && num != 1)
// if both conditions returns true
return true;
// if any condition becomes false
return false;
}
//driver code
public static void main (String args[])
{
long num1, num2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the start number: ");
//input from the user
num1=sc.nextLong();
System.out.println("Enter the end number: ");
//input from the user
num2=sc.nextLong();
System.out.println("Perfect numbers between "+num1+" and "+num2+" are: ");
//loop executes until the condition num1 < num2 becomes false
for (num1 = 2; num1 < num2; num1++)
//calling function
if(chkPerfectNumber(num1))
//prints all perfect number between given range
System.out.println(num1 +" is a perfect number");
}
}
Output of the above code:
Enter the start number: 2
Enter the end number: 10000
Perfect numbers between 2 and 10000 are:
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java