Java Topics
- Java Intro
- Java String Methods
- Java Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Perfect Number
- Perfect Square
- Power of 2
- Power of 3
- Pronic Number
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- Java Star Pattern
- Java Number Pattern
- Java Alphabet Pattern
Java Program to Check Perfect Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, exploring the properties of numbers is a fascinating task. One such special type of number is a perfect number.
A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
In this tutorial, we will delve into a Java program designed to check whether a given number is a perfect number.
The program involves finding the divisors of a number and determining if their sum equals the original number.
π Example
Let's delve into the Java code that accomplishes this task.
import java.util.Scanner;
public class PerfectNumberChecker {
// Function to check if a number is a perfect number
static boolean isPerfectNumber(int number) {
int sum = 1; // Start with 1 as every number is divisible by 1
// Iterate from 2 to the square root of the number
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
// If i is a divisor, add i and its counterpart to the sum
sum += i;
if (i * i != number) {
sum += number / i;
}
}
}
// If the sum equals the original number, it is a perfect number
return sum == number;
}
// Driver program
public static void main(String[] args) {
// Replace this value with the number you want to check
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Call the function to check if the number is a perfect number
if (isPerfectNumber(number)) {
System.out.println(number + " is a perfect number.");
} else {
System.out.println(number + " is not a perfect number.");
}
}
}
π» Testing the Program
To test the program with different numbers, enter a new value when prompted.
Enter a number: 28 28 is a perfect number.
π§ How the Program Works
- The program defines a class PerfectNumberChecker containing a static method isPerfectNumber that takes an integer number as input and returns true if the number is a perfect number, and false otherwise.
- Inside the main method, replace the value of number with the desired number you want to check.
- The program calls the isPerfectNumber method and prints the result.
π Between the Given Range
Let's delve into the java code that identifies perfect numbers in the specified range.
public class PerfectNumberChecker {
// Function to check if a number is perfect
static boolean isPerfectNumber(int num) {
int sum = 1; // Start with 1 as every number is divisible by 1
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}
return sum == num;
}
// Driver program
public static void main(String[] args) {
System.out.println("Perfect Numbers in the range 1 to 50: ");
for (int i = 2; i <= 50; i++) {
if (isPerfectNumber(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}
}
π» Testing the Program
Perfect Numbers in the range 1 to 50: 6 28
Compile and run the program to see the perfect numbers in the specified range.
π§ How the Program Works
- The program defines a class PerfectNumberChecker containing a static method isPerfectNumber to check if a number is perfect.
- Inside the method, it iterates through divisors up to the square root of the number and calculates the sum of proper divisors.
- The main method tests for perfect numbers in the range from 1 to 50 and prints the results.
π§ Understanding the Concept of Perfect Number
Before delving into the code, let's understand the concept of perfect numbers. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing alternative approaches or optimizations for checking perfect numbers.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Java Program to Check Perfect Number), please comment here. I will help you immediately.