Java Basic
Java Interview Programs
- Java Interview Programs
- Java Abundant Number
- Java Amicable Number
- Java Armstrong Number
- Java Average of N Numbers
- Java Automorphic Number
- Java Biggest of three numbers
- Java Binary to Decimal
- Java Common Divisors
- Java Composite Number
- Java Condense a Number
- Java Cube Number
- Java Decimal to Binary
- Java Decimal to Octal
- Java Disarium Number
- Java Even Number
- Java Evil Number
- Java Factorial of a Number
- Java Fibonacci Series
- Java GCD
- Java Happy Number
- Java Harshad Number
- Java LCM
- Java Leap Year
- Java Magic Number
- Java Matrix Addition
- Java Matrix Division
- Java Matrix Multiplication
- Java Matrix Subtraction
- JS Matrix Transpose
- Java Maximum Value of an Array
- Java Minimum Value of an Array
- Java Multiplication Table
- Java Natural Number
- Java Number Combination
- Java Odd Number
- Java Palindrome Number
- Java Pascalβs Triangle
- Java Perfect Number
- Java Perfect Square
- Java Power of 2
- Java Power of 3
- Java Pronic Number
- Java Prime Factor
- Java Prime Number
- Java Smith Number
- Java Strong Number
- Java Sum of Array
- Java Sum of Digits
- Java Swap Two Numbers
- Java Triangular Number
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.