Java Program to Check Whether a Given Number is Perfect Number

A perfect number in Java is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself. For example, the divisors of 6 are 1, 2 and 3. The sum of the proper divisors of 6 is 1 + 2 + 3 = 6, which is a perfect number. The sum of the proper divisors of 28 is 1 + 2 + 4 + 7 + 14 = 28, which is also a perfect number.

Problem Description

Write a Java Program that will ask the user for a number and then check whether the number is a perfect number or not.

Problem Solution

1. Take the number as input and store the number in a variable.
2. Loop through all the numbers from 1 to the number and check whether the number is a divisor of the number.
3. If the number is a divisor of the number, add it to the sum.
4. If the sum is equal to the number, print “The number is a perfect number”.
5. If the sum is not equal to the number, print “The number is not a perfect number”.

Program/Source Code

Here is the source code of the Java Program to Check if a given Number is Perfect Number. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Perfect
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int n, sum = 0;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter the number: ");
  9.         n = s.nextInt();
  10.         for(int i = 1; i < n; i++)
  11.         {
  12.             if(n % i == 0)
  13.             {
  14.                 sum = sum + i;
  15.             }
  16.         }
  17.         if(sum == n)
  18.         {
  19.             System.out.println("Given number is a perfect number");
  20.         }
  21.         else
  22.         {
  23.             System.out.println("Given number is not a perfect number");
  24.         }    
  25.     }
  26.     int divisor(int x)
  27.     {
  28.        return x;
  29.     }
  30. }
Program Explanation

1. Take the number as input and store the number in a variable n.
2. Loop through all the numbers from 1 to the number and check whether the number is a divisor of the number.
3. If the number is a divisor of the number, add it to the sum.
4. If a condition is true, print “The number is a perfect number,” otherwise print “The number is not a perfect number.”

advertisement
advertisement

Time Complexity: O(n)
The above program for checking whether a number is perfect or not has a time complexity of O(n), where n is the number given as input.

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

Testcase 1: In this case, we enter the number “6” as input to determine whether it is a perfect number or not.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Enter the number: 6
Given number is a perfect number

Testcase 2: In this case, we enter the number “34” as input to determine whether it is a perfect number or not.

Enter the number: 34
Given number is not a perfect number

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.