Exams
Test Series
Previous Year Papers
Syllabus
Books
Cut Off
Latest Updates
Eligibility
Perfect Numbers in Java: Perfect Number Programs with Solved Examples
IMPORTANT LINKS
In math, a perfect number is a positive whole number that is equal to the sum of all its positive divisors, except itself. For example, the divisors of 6 are 1, 2, and 3 (excluding 6), and when we add them: 1 + 2 + 3 = 6. So, 6 is a perfect number. Other examples include 28, 496, and 8128.
Java is a widely-used, object-oriented programming language first developed by Sun Microsystems in 1995. Now owned by Oracle, Java runs on over 3 billion devices and supports many operating systems like Windows, macOS, and Linux.
Maths Notes Free PDFs
Topic | PDF Link |
---|---|
Class 12 Maths Important Topics Free Notes PDF | Download PDF |
Class 10, 11 Mathematics Study Notes | Download PDF |
Most Asked Maths Questions in Exams | Download PDF |
Increasing and Decreasing Function in Maths | Download PDF |
In this article, we’ll explore how to check for perfect numbers using Java. You will learn how to write simple Java programs to check whether a number is perfect or not. We’ll also write a program to find all perfect numbers within a given range.
What is a Perfect Number in Java?
In Java, a number is called a perfect number if the sum of all its positive divisors, excluding the number itself, is equal to the number. For example, 28 is a perfect number. The divisors of 28 are 1, 2, 4, 7, 14, and 28. If we leave out 28 and add the rest: 1 + 2 + 4 + 7 + 14 = 28. Since the sum is equal to the number itself, 28 is a perfect number. While writing a Java program, we use this rule to check whether a number is perfect or not.
Example of a Perfect Number:
Let’s take the number
First, find the factors of
Then find the sum of factors, i.e.,
Now, observe that the sum of factors is equal to the number itself. Hence, the number
Here are a few more examples of perfect numbers:
Perfect Numbers |
Positive Factors |
Sum of all factors excluding itself |
|
|
|
|
|
|
Steps to Find Perfect Number in Java
Follow the below steps to find the perfect number in Java:
Step 1: Read or initialize a number (
Step 2: Declare a variable (
Step 3: Find the factors of the given number (
Step 4: Calculate the sum of factors and store it in a variable
Step 5: Compare the sum (
- If both (
and ) are equal, then the given number is a perfect number. - Else, the number is not a perfect number.
Let’s implement the above steps in a Java program to find the perfect numbers.
- 3 Live Test
- 163 Class XI Chapter Tests
- 157 Class XII Chapter Tests
Perfect Number in Java Programs
There are the following three ways to find the perfect number in Java:
- Using while loop
- Using static method
- Using recursion
Using while Loop:
The program to find the perfect numbers in Java using while loop is given below:
import java.util.Scanner;
public class PerfectNumberExample
{
public static void main(String args[])
{
long n, sum=0;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number: “);
n=sc.nextLong();
int i=1;
/executes until the condition becomes false
while(i <= n/2)
{
if(n % i == 0)
{
/calculates the sum of factors
Sum = sum + i;
} /end of if
/after each iteration, increments the value of variable i by 1
i++;
} /end of while
/compares sum with the number
if(sum==n)
{
/prints if sum and n are equal
System.out.println(n+” is a perfect number.”);
} /end of if
else
/prints if sum and n are not equal
System.out.println(n+” is not a perfect number.”);
}
}
Output 1:
Enter the number:
Output 2:
Enter the number:
Using Static Method:
The program to find the perfect numbers in Java using static method is given below:
import java.util.Scanner;
public class PerfectNumberExample
{
/function that checks if the given number is perfect or not
static long isPerfect(long num)
{
/variable stores the sum
long sum=0;
/executes until the condition becomes false
for(int i=1; i <= num/2; i++)
{
if(num % i == 0)
{
/calculates the sum of factors
sum=sum + i;
} /end of if
} /end of for
/returns the sum of factors
return sum;
} /end of method
public static void main(String args[])
{
long number, s;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number: “);
/reads a number from the user
number=sc.nextLong();
/calling the function
s = isPerfect(number);
/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 1:
Enter the number:
Output 2:
Enter the number:
Using Recursion:
The program to find the perfect numbers in Java using recursion method is given below:
import java.util.Scanner;
public class PerfectNumberExample
{
static long sum=0;
long isPerfect(long num, int i)
{
/executes until the condition becomes false
if(i <= num/2)
{
if(num % i ==0)
{
/calculates the sum of factors
sum=sum + i;
}
/after each iteration, increments the value of variable i by 1
i++;
/recursively called function
isPerfect(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: “);
/reads a number from the user
number=sc.nextLong();
/creating an object of the class
PerfectNumberExample pne=new PerfectNumberExample3( );
s = pne.isPerfect(number, i);
/compares sum with the number
if(s == number)
/prints if the s and number are equal
System.out.println(number+” is a perfect number”);
else
/prints if s and number are not equal
System.out.println(number+” is not a perfect number”);
}
}
Output 1:
Enter the number:
Output 2:
Enter the number:
Find and Print Perfect Numbers between 2 to 1000
The program to find and print the perfect numbers between a given range (for example, between 2 to 1000) in Java is given below:
public class PerfectNumberExample
{
/function checks if the given number is perfect or not
static boolean isPerfectNumber(int num)
{
/variable stores the sum of divisors
int sum = 1;
/determines all the divisors of the given number and adds 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;
} /end of if
} /end of for
if (sum == num && num != 1)
/returns true if both conditions (above) returns true
return true;
/returns false if any condition becomes false
return false;
} /end of function
/driver code
public static void main (String args[])
{
System.out.println(“Perfect Numbers between 2 to 1000 are: “);
/loop executes until the condition n<1000 becomes false
for (int n = 2; n < 1000; n++)
/calling function
if (isPerfectNumber(n))
/prints all perfect number between given range
System.out.println(n +” is a perfect number”);
}
}
Output:
Perfect Numbers between
Perfect Numbers in Java Key Points to Remember
- Any number can be a perfect number in Java if the sum of its positive divisors excluding the number itself is equal to that number.
- Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java.
- There are three ways to find the perfect number in Java, i.e., using while loop, using static method, and using recursion.
- The perfect number is important because mathematically, perfect numbers give a good number theory example to the general idea of classification, i.e. all even perfects have a specific form.
- The first five perfect numbers are
, , , , and .
We hope that the above article is helpful for your understanding and exam preparations. Stay tuned to the Testbook App for more updates on related topics from Mathematics, and various such subjects. Also, reach out to the test series available to examine your knowledge regarding several exams.
Perfect Numbers in Java FAQs
Why is 4 not a perfect number?
The
What is the logic of the perfect number?
The logic of the perfect number is that a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example,
Why is the perfect number important?
The perfect number is important because mathematically, perfect numbers give a good number theory example to the general idea of classification, i.e. all even perfects have a specific form.
What are the first five perfect numbers?
The first five perfect numbers are
Why is 6 a perfect number?
The
What is the time complexity of checking a perfect number?
It is O(n), because we check all numbers less than the given number.
What is the importance of perfect numbers in programming?
They are often used in math puzzles, algorithm challenges, and number theory concepts in programming practice.