In java, to check if a number is Perfect or not we can use loop concepts such as while loop or for loop. In this article, we are focusing on learning through examples, detailed logic and program explanation for better understanding.
What is Perfect Number?
A perfect number is defined as a positive integer that is equal to the sum of its proper divisors, excluding itself. For example, the number 6 is a perfect number because its proper divisors (1, 2, and 3) sum to 6 (1 + 2 + 3 = 6).
In the case of the number 2, its only proper divisor is 1, and 1 does not equal 2. Therefore, 2 is not a perfect number. The smallest perfect number is actually 6.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We need to write a java program to check a given number is Perfect or not. The program should take an input integer number as num and check if the sum of factors is equal to original number. When sum of factors is equal to num, we say it as Perfect number.
Let’s see an example to understand it,
Example 1:
For input number 28, Output should be “Perfect number”.
Explanation:
Input integer number =28
Factors of 28 = 1,2,4,7,14
Calculating sum = 1+2+4+7+14 =28
Hence, input number (28) is equal to the sum of factors of the input number. Therefore, 28 is a Perfect number.
Example 2:
For input integer number 8, Output should be “Not a Perfect number”.
Explanation:
Input integer number =8
Factors of 28 = 1,2,4
Calculating sum = 1+2+4 = 7
Hence, input number (8) is not equal to sum of the factors of the number 8. Therefore, 8 is not a Perfect number.
Logic to check a given number is Perfect or not
There are multiple logics to check if a given number is perfect or not in Java Programming language.
Logic 1: Using while loop
- The program iterates through potential divisors up to half the number (num/2) using a while loop.
- Inside the loop, it checks if each potential divisor divides evenly into the number using the modulo operator (num%i==0).
- If a divisor is found, it’s added to the sum. Finally, it compares the sum of all divisors with the original number.
- If the sum is equal to the number, then it’s a perfect number.
Logic 2: Using for loop
- The program iterates through potential divisors from 1 to the number minus 1 (i<num) using a for loop. This ensures it doesn’t check the number itself as a divisor.
- Inside the loop, it checks if the current value of i divides evenly into the number num using the modulo operator (num%i==0).
- If a divisor is found, its value is added to the sum variable. After the loop completes, it compares the sum of all divisors (sum) with the original number (num).
- If the sum is equal to the number, it’s a perfect number because the sum represents all its proper divisors (excluding itself).
Java program to check a given number is Perfect or not
Program 1: Using while loop
In the below programming we are using while loop to check a given number is Perfect or not.
import java.util.Scanner;
public class PerfectNumCheck {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter an Integer number");
int num= s.nextInt();
int sum=0;
int i=1;
while(i<=num/2) {
if(num%i==0) {
sum+=i;
}
i++;
}
if(sum==num)
System.out.println(num + " is Perfect Number");
else
System.out.println(num+" is not a Perfect Number");
s.close();
}
}
Output:
Enter an Integer number
8128
8128 is Perfect Number
Java program explanation to check a given number is Perfect or not
- The above java program imports the Scanner class from java.util package for user input.
- The main method is the entry point of the program.
- Program asks the user to enter a number.
- It reads the input number using Scanner and stores it in the variable
num
. - Program will initializes variables
sum
andi
to 0 and 1 respectively.- It enters a while loop that iterates while
i
is less than or equal to half of the given numbernum
.
- It enters a while loop that iterates while
- Inside the loop, it checks if
num
is divisible byi
without any remainder (num % i == 0
). If it is, it addsi
to thesum
. - It increments
i
by 1 in each iteration. - After the loop, it checks if the sum of the factors (
sum
) is equal to the original number (num
). - If the sum is equal to
num
, it prints thatnum
is a perfect number. Otherwise, it prints that it’s not a perfect number. - Program closes the Scanner object to release system resources.
Program 2: Using for loop
In the below programming we are using for loop to check a given number is Perfect or not.
import java.util.Scanner;
public class PerfectNumCheck {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter an Integer number");
int num= s.nextInt();
int sum=0;
for(int i=1;i<num; i++) {
if(num%i==0) {
sum+=i;
}
}
if(sum==num)
System.out.println(num + " is Perfect Number");
else
System.out.println(num+" is not a Perfect Number");
s.close();
}
}
Output:
Enter an Integer number
33550336
33550336 is Perfect Number
Java program explanation to check a given number is Perfect or not
- The java program imports the Scanner class from java.util package for user input.
- The main method is the entry point of the program.
- Program asks the user to enter a number.
- It reads the input number using Scanner and stores it in the variable
num
. - Program initializes variables
sum
to 0. - It enters a for loop that iterates from 1 to one less than the given number
num
. - Inside the loop, it checks if
num
is divisible by the loop variablei
without any remainder (num % i == 0
). If it is, it addsi
to thesum
. - After the loop, it checks if the sum of the factors (
sum
) is equal to the original number (num
). - If the sum is equal to
num
, it prints thatnum
is a perfect number. Otherwise, it prints that it’s not a perfect number. - Program closes the Scanner object to release system resources.
Conclusion
In this tutorial, we have learnt writing the Java program to check perfect number. We have seen the writing the program using while loop in java and for loop in java. Hope this tutorial helped you in the understanding of the program.
You can also check our another awesome tutorials:
- Count the digits of a given number in Java
- Java program to print sum of odd numbers in given range
- Check a triangle is equilateral, isosceles or scalene in Java
- Java program to print sum of even numbers in given range
- Java program to print all odd numbers in given range
Happy Coding!!