
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Given Number is Perfect Number in Java
When the sum of factors of a given number (after discarding the given number) is equal to the number itself is called a perfect number. The sum of these divisors is known as the aliquot sum. Thus, a number is perfect if it matches its aliquot sum. Notably, all known perfect numbers are even.
Problem Statement
In this article, we will create Java programs to check if a given number is perfect or not. For the given problem we are going to use iterative approaches like for loop and while loop. Let's try to understand through some examples ?
Input 1
Given number: 496
Output 1
Its factors are: 1, 2, 4, 8, 16, 31, 62, 124, and 248 ( we have to exclude 496 )
Sum of the factors are: 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496
Therefore, it is a perfect number
Input 2
Given number: 54
Output 2
Its factors are: 1, 2, 3, 6, 9, 18, and 27 ( we have to exclude 54 )
Sum of the factors are: 1 + 2 + 3 + 6 + 9 + 18 + 27 = 66
Therefore, it is not a perfect number
Approaches to check perfect number
Below are some approaches through which we can check if a given number is the perfect number ?
Check Perfect Number Using For loop
The for loop is the entry control loop where the given condition is executed.
Syntax
for ( initial expression; conditional expression; increment/decrement expression ) { // code to be executed }
initial expression ? executed once when loop begins.
conditional expression ? code will be executed till conditional expression is true.
increment/decrement expression ? to increment/decrement loop variable.
Steps to check if a given number is the perfect number using for loop
Following are the steps to check if a given number is the perfect number using for loop ?
- Import the classes from java.util package.
- Declare and initialize an integer variable n1 for the number to check and another integer variable add to store the sum of its factors.
- Use a for loop that runs from 1 to n1 - 1.
- Inside the loop, use an if statement to check if n1 is divisible by the current loop variable.
- If divisible, add the loop variable to add.
- After the loop, use an if-else block to check if add is equal to n1.
Example
import java.util.*; public class Perfect { public static void main(String[] args) { int n1 = 496; int add = 0; for(int i = 1; i < n1; i++) { if(n1 % i==0) { add = add + i; // adding and incrementing } } boolean isPerfect = (add == n1); if(isPerfect) { System.out.println("is " + n1 + " a perfect number?: " + isPerfect); } else { System.out.println("is " + n1 + " a perfect number?: " + isPerfect); } } }
Output
is 496 a perfect number?: true
Check Perfect Number Using While loop
The while loop is an entry control loop, where the condition is checked before executing the loop's body.
Syntax
while (conditional expression) { // code will be executed till conditional expression is true increment/decrement expression; // to increment or decrement loop variable }
Steps to check if a given number is the perfect number using while loop
Following are the steps to check if a given number is the perfect number using while loop ?
- Import the classes from java.util package.
- Declare and initialize an integer variable n1 for the number to check and another integer variable add to store the sum of its factors.
- Declare a loop variable i and set it to 1.
- Use a while loop that runs as long as i is less than n1.
- Inside the loop, use an if statement to check if n1 is divisible by i. If divisible, add i to add.
- Increment i by 1. After the loop, use an if-else block to check if add is equal to n1.
Example
import java.util.*; public class Main { public static void main(String[] args) { int n1 = 28; int add = 0; int i = 1; // loop variable while(i < n1) { if(n1 % i == 0) { add = add + i; } i++; // incrementing } boolean isPerfect = (add == n1); if(isPerfect) { System.out.println("is " + n1 + " a perfect number?: " + isPerfect); } else { System.out.println("is " + n1 + " a perfect number?: " + isPerfect); } } }
Output
is 28 a perfect number?: true
In the above program, we have followed the same logic but with a different value of variable n1, and instead of a for loop we have used a while loop.
Check Perfect Number by Running the Loop Till n/2
This approach is more optimized than the other two approaches we have discussed earlier in this article. In this approach, the loop will iterate till half of the given number only because we can find all factors of a number (excluding the number itself) in between half of that number.
Steps to check if a given number is the perfect number iterating till n/2
Following are the steps to check if a given number is the perfect number iterating till n/2 ?
- Declare and initialize an integer variable n1 for the number to check and another integer variable add to store the sum of its factors.
- Declare a loop variable i and set it to 1.
- Use a while loop that runs as long as i is less than or equal to n1 / 2.
- Inside the loop, use an if statement to check if n1 is divisible by i. If divisible, add i to add. Increment i by 1.
- After the loop, use an if-else block to check if add is equal to n1.
Example
import java.util.*; public class Perfect { public static void main(String[] args) { int n1=6; int add = 0; int i=1; while(i <= n1/2) { // loop will run till 3 ( 6/2 = 3) if(n1 % i==0) { add = add + i; } i++; } boolean isPerfect = (add == n1); if(isPerfect) { System.out.println("is " + n1 + " a perfect number?: " + isPerfect); } else { System.out.println("is " + n1 + " a perfect number?: " + isPerfect); } } }
Output
is 6 a perfect number?: true
Conclusion
In this article, we have seen three approaches of Java programs to check if a given number is perfect or not. We understood how to use iterative approaches to make a Java program. The approach 3 is more optimized and recommended by us.