Overview
Test Series
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.
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.
Let’s take the number \(496\) and check if it is a perfect number or not.
First, find the factors of \(496\), i.e. \(1\), \(2\), \(4\), \(8\), \(16\), \(31\), \(62\), \(124\), and \(248\).
Then find the sum of factors, i.e., \(1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496\).
Now, observe that the sum of factors is equal to the number itself. Hence, the number \(496\) is a perfect number. Similarly, we can check other numbers also.
Here are a few more examples of perfect numbers:
Perfect Numbers |
Positive Factors |
Sum of all factors excluding itself |
\(6\) |
\(1, 2, 3, 6\) |
\(6\) |
\(8128\) |
\(1, 2, 4, 8, 16, 32, 64, 127, 254, 508, 1016, 2032, 4064, 8128\) |
\(8128\) |
Follow the below steps to find the perfect number in Java:
Step 1: Read or initialize a number (\(n\)).
Step 2: Declare a variable (\(s\)) for storing sum.
Step 3: Find the factors of the given number (\(n\)) by using a loop (for or while).
Step 4: Calculate the sum of factors and store it in a variable \(s\).
Step 5: Compare the sum (\(s\)) with the number (\(n\)):
Let’s implement the above steps in a Java program to find the perfect numbers.
There are the following three ways to find the perfect number in Java:
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: \(6\)
\(6\) is a perfect number.
Output 2:
Enter the number: \(1234\)
\(1234\) is not a perfect number.
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: \(28\)
\(28\) is a perfect number.
Output 2:
Enter the number: \(4558\)
\(4558\) is not a perfect number.
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: \(8128\)
\(8128\) is a perfect number.
Output 2:
Enter the number: \(7866\)
\(7866\) is not a perfect number.
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 \(2\) and \(1000\) are:
\(6\) is a perfect number.
\(28\) is a perfect number.
\(496\) is a perfect number.
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.
Download the Testbook APP & Get Pass Pro Max FREE for 7 Days
Download the testbook app and unlock advanced analytics.