Exams
Test Series
Previous Year Papers
JEE Main Previous Year Question Paper JEE Advanced Previous Year Papers NEET Previous Year Question Paper CUET Previous Year Papers COMEDK UGET Previous Year Papers UP Polytechnic Previous Year Papers AP POLYCET Previous Year Papers TS POLYCET Previous Year Papers KEAM Previous Year Papers MHT CET Previous Year Papers WB JEE Previous Year Papers GUJCET Previous Year Papers ICAR AIEEA Previous Year Papers CUET PG Previous Year Papers JCECE Previous Year Papers Karnataka PGCET Previous Year Papers NEST Previous Year Papers KCET Previous Year Papers LPUNEST Previous Year Papers AMUEEE Previous Year Papers IISER IAT Previous Year Papers Bihar Diploma DECE-LE Previous Year Papers NPAT Previous Year Papers JMI Entrance Exam Previous Year Papers PGDBA Exam Previous Year Papers AP ECET Previous Year Papers PU CET Previous Year Papers GPAT Previous Year Papers CEED Previous Year Papers AIAPGET Previous Year Papers JKCET Previous Year Papers HPCET Previous Year Papers CG PAT Previous Year Papers SRMJEEE Previous Year Papers BCECE Previous Year Papers AGRICET Previous Year Papers TS PGECET Previous Year Papers MP PAT Previous Year Papers IIT JAM Previous Year Papers CMC Vellore Previous Year Papers ACET Previous Year Papers TS EAMCET Previous Year Papers NATA Previous Year Papers AIIMS MBBS Previous Year Papers BITSAT Previous Year Papers JEXPO Previous Year Papers HITSEEE Previous Year Papers AP EAPCET Previous Year Papers UCEED Previous Year Papers CG PET Previous Year Papers OUAT Previous Year Papers VITEEE Previous Year Papers
Syllabus
JEE Main Syllabus JEE Advanced Syllabus NEET Syllabus CUET Syllabus COMEDK UGET Syllabus UP Polytechnic JEECUP Syllabus AP POLYCET Syllabus TS POLYCET Syllabus KEAM Syllabus MHT CET Syllabus WB JEE Syllabus OJEE Syllabus ICAR AIEEA Syllabus CUET PG Syllabus NID Syllabus JCECE Syllabus Karnataka PGCET Syllabus NEST Syllabus KCET Syllabus UPESEAT EXAM Syllabus LPUNEST Syllabus PUBDET Syllabus AMUEEE Syllabus IISER IAT Syllabus NPAT Syllabus JIPMER Syllabus JMI Entrance Exam Syllabus AAU VET Syllabus PGDBA Exam Syllabus AP ECET Syllabus GCET Syllabus CEPT Syllabus PU CET Syllabus GPAT Syllabus CEED Syllabus AIAPGET Syllabus JKCET Syllabus HPCET Syllabus CG PAT Syllabus BCECE Syllabus AGRICET Syllabus TS PGECET Syllabus BEEE Syllabus MP PAT Syllabus MCAER PG CET Syllabus VITMEE Syllabus IIT JAM Syllabus CMC Vellore Syllabus AIMA UGAT Syllabus AIEED Syllabus ACET Syllabus TS EAMCET Syllabus PGIMER Exam Syllabus NATA Syllabus AFMC Syllabus AIIMS MBBS Syllabus BITSAT Syllabus BVP CET Syllabus JEXPO Syllabus HITSEEE Syllabus AP EAPCET Syllabus GITAM GAT Syllabus UPCATET Syllabus UCEED Syllabus CG PET Syllabus OUAT Syllabus IEMJEE Syllabus VITEEE Syllabus SEED Syllabus MU OET Syllabus
Books
Cut Off
JEE Main Cut Off JEE Advanced Cut Off NEET Cut Off CUET Cut Off COMEDK UGET Cut Off UP Polytechnic JEECUP Cut Off AP POLYCET Cut Off TNEA Cut Off TS POLYCET Cut Off KEAM Cut Off MHT CET Cut Off WB JEE Cut Off ICAR AIEEA Cut Off CUET PG Cut Off NID Cut Off JCECE Cut Off Karnataka PGCET Cut Off NEST Cut Off KCET Cut Off UPESEAT EXAM Cut Off AMUEEE Cut Off IISER IAT Cut Off Bihar Diploma DECE-LE Cut Off JIPMER Cut Off JMI Entrance Exam Cut Off PGDBA Exam Cut Off AP ECET Cut Off GCET Cut Off CEPT Cut Off PU CET Cut Off CEED Cut Off AIAPGET Cut Off JKCET Cut Off HPCET Cut Off CG PAT Cut Off SRMJEEE Cut Off TS PGECET Cut Off BEEE Cut Off MP PAT Cut Off VITMEE Cut Off IIT JAM Cut Off CMC Vellore Cut Off ACET Cut Off TS EAMCET Cut Off PGIMER Exam Cut Off NATA Cut Off AFMC Cut Off AIIMS MBBS Cut Off BITSAT Cut Off BVP CET Cut Off JEXPO Cut Off HITSEEE Cut Off AP EAPCET Cut Off GITAM GAT Cut Off UCEED Cut Off CG PET Cut Off OUAT Cut Off VITEEE Cut Off MU OET Cut Off
Latest Updates
Eligibility
JEE Main Eligibility JEE Advanced Eligibility NEET Eligibility CUET Eligibility COMEDK UGET Eligibility UP Polytechnic JEECUP Eligibility TNEA Eligibility TS POLYCET Eligibility KEAM Eligibility MHT CET Eligibility WB JEE Eligibility OJEE Eligibility ICAR AIEEA Eligibility CUET PG Eligibility NID Eligibility JCECE Eligibility Karnataka PGCET Eligibility NEST Eligibility KCET Eligibility LPUNEST Eligibility PUBDET Eligibility AMUEEE Eligibility IISER IAT Eligibility Bihar Diploma DECE-LE Eligibility NPAT Eligibility JIPMER Eligibility JMI Entrance Exam Eligibility AAU VET Eligibility PGDBA Exam Eligibility AP ECET Eligibility GCET Eligibility CEPT Eligibility PU CET Eligibility GPAT Eligibility CEED Eligibility AIAPGET Eligibility JKCET Eligibility HPCET Eligibility CG PAT Eligibility SRMJEEE Eligibility BCECE Eligibility AGRICET Eligibility TS PGECET Eligibility MP PAT Eligibility MCAER PG CET Eligibility VITMEE Eligibility IIT JAM Eligibility CMC Vellore Eligibility AIMA UGAT Eligibility AIEED Eligibility ACET Eligibility PGIMER Exam Eligibility CENTAC Eligibility NATA Eligibility AFMC Eligibility AIIMS MBBS Eligibility BITSAT Eligibility JEXPO Eligibility HITSEEE Eligibility AP EAPCET Eligibility GITAM GAT Eligibility UPCATET Eligibility UCEED Eligibility CG PET Eligibility OUAT Eligibility IEMJEE Eligibility SEED Eligibility MU OET Eligibility

Perfect Numbers in Java: Perfect Number Programs with Solved Examples

Last Updated on Jul 04, 2025
Download As PDF
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.

UGC NET/SET Course Online by SuperTeachers: Complete Study Material, Live Classes & More

Get UGC NET/SET SuperCoaching @ just

₹25999 ₹11666

Your Total Savings ₹14333
Explore SuperCoaching

Example of a Perfect Number:

Let’s take the number and check if it is a perfect number or not.

First, find the factors of , i.e. , , , , , , , , and .

Then find the sum of factors, i.e., .

Now, observe that the sum of factors is equal to the number itself. Hence, the number 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

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 () for storing sum.

Step 3: Find the factors of the given number () by using a loop (for or while).

Step 4: Calculate the sum of factors and store it in a variable .

Step 5: Compare the sum () with the number ():

  • 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.

Test Series
134.1k Students
NCERT XI-XII Physics Foundation Pack Mock Test
323 TOTAL TESTS | 3 Free Tests
  • 3 Live Test
  • 163 Class XI Chapter Tests
  • 157 Class XII Chapter Tests

Get Started

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:

is a perfect number.

Output 2: 

Enter the number:

is not a perfect 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:

is a perfect number.

Output 2: 

Enter the number:

is not a perfect 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:

is a perfect number.

Output 2: 

Enter the number:

is not a perfect 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 and are:

is a perfect number.

is a perfect number.

is a perfect number.

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.

Important Links
NEET Exam
NEET Previous Year Question Papers NEET Mock Test NEET Syllabus
CUET Exam
CUET Previous Year Question Papers CUET Mock Test CUET Syllabus
JEE Main Exam
JEE Main Previous Year Question Papers JEE Main Mock Test JEE Main Syllabus
JEE Advanced Exam
JEE Advanced Previous Year Question Papers JEE Advanced Mock Test JEE Advanced Syllabus

More Articles for Maths

Perfect Numbers in Java FAQs

The is not a perfect number because the sum of its factors (besides itself), , is less than . Numbers like are known as deficient numbers.

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, has divisors , , and , and , so is a perfect number.

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 .

The is a perfect number because has divisors , , and , and , so is a perfect number.

It is O(n), because we check all numbers less than the given number.

They are often used in math puzzles, algorithm challenges, and number theory concepts in programming practice.

Report An Error