Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java Program to Check Perfect Number

Posted in Java Tutorial
Updated on Oct 30, 2024
By Mari Selvan
πŸ‘οΈ 95 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
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.

PerfectNumberChecker.java
Copied
Copy To Clipboard
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.

Output
Enter a number: 28
28 is a perfect number.

🧠 How the Program Works

  1. 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.
  2. Inside the main method, replace the value of number with the desired number you want to check.
  3. 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.

PerfectNumberChecker.java
Copied
Copy To Clipboard
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

Output
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

  1. The program defines a class PerfectNumberChecker containing a static method isPerfectNumber to check if a number is perfect.
  2. Inside the method, it iterates through divisors up to the square root of the number and calculates the sum of proper divisors.
  3. 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (Java Program to Check Perfect Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy