Java program to check a perfect number or not. The following program has been written in 3 different ways to check whether the given number is a perfect number or not. The compiler is also added so that you can execute the program yourself, along with sample outputs citing few examples. The methods used are:
- Print using While Loop.
- Print using Static Method.
- Print using Recursion.
A Perfect number is a positive integer that is equal to the sum of its proper divisors except itself.
Let’s take an easy example, such as 6.
1, 2, 3 and 6 are the divisors of 6. If we add up all the numbers except 6, we end with the sum of 6 itself.
1 + 2 + 3 = 6 = Perfect Number.
Here are a few more examples of perfect numbers:
As you can see, these are regarded as even perfect numbers. An even perfect number is a perfect number that is even, i.e., an even number n whose sum of divisors (including n itself) equals n.
All known perfect numbers are even. Ochem and Rao (2012) have demonstrated that any odd perfect number must be larger than 10^1500.
Perfect Number – Using While Loop
1) The number which is equal to the sum of its divisors is called a perfect number.
2) Read the entered long number, assigned to the long variable n.
3) while loop iterates until the condition (i<=n/2) is false. If the remainder of n/i=0 then add i value to sum and increase the i value. After all the iterations compare the number with the sum, if both are equal then prints the number is a perfect number.
4) Example n=6,i=1 condition at while is 1<=3 is true, here 6%i=0 is true so sum=1,
i=2, 2<3 is true, 6%2=0 is true so sum=1+2=3,
i=3, 3<=3 is true ,6%3=0 is true so sum=3+3=6, for i=4 the while loop terminate and compare the sum value with n, both are equal, so it prints 6 is a perfect number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Scanner; class Perfect { public static void main(String arg[]) { long n,sum=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextLong(); int i=1; while(i<=n/2) { if(n%i==0) { sum+=i; } i++; } if(sum==n) { System.out.println(n+" is a perfect number"); } else System.out.println(n+" is not a perfect number"); } } |
Output:
1 2 3 |
Enter a number 6 6 is a perfect number |
Using Static Method
1) In this program we have a static method long perfectOrNot(long num), it calculates the sum of proper divisors of the given number.
2) Call the method long perfectOrNot(long num) in the main method as perfectOrNot(n). Then that method executes the code and returns the sum value, the sum will be assigned to the variable p. Now compare the p with the given number n, if both are equal then it prints a number is a perfect number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class Perfect { public static void main(String arg[]) { long n,p; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextLong(); p=perfectOrNot(n); if(p==n) System.out.println(n+" is a perfect number"); else System.out.println(n+" is not a perfect number"); } static long perfectOrNot(long num) { long sum=0; for(int i=1;i<=num/2;i++) { if(num%i==0) { sum+=i; } } return sum; } } |
Output:
1 2 3 |
Enter a number 50 50 is not a perfect number |
Perfect Number – Using Recursion
1) Call the method perfectOrNot() using the Perfect class object as p.perfectOrNot(n,i), then perfectOrNot(n,i) method starts the execution and calls itself as perfectOrNot(num,i); it repeats until the condition i<=num/2 is false.
2) This method returns the sum value and compares the sum with the original number, if both are equal then that number is called a perfect number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Scanner; class Perfect { static long sum=0; long perfectOrNot(long num,int i) { if(i<=num/2) { if(num%i==0) { sum+=i; } i++; perfectOrNot(num,i); } return sum; } public static void main(String arg[]) { long n,res; int i=1; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextLong(); Perfect p=new Perfect( ); res=p.perfectOrNot(n,i); if(res==n) System.out.println(n+" is a perfect number"); else System.out.println(n+" is not a perfect number"); } } |
Output:
1 2 3 |
Enter a number 55 55 is not a perfect number |
More Java Programs: