Perfect number

This Java code example demonstrates how to check if a given number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. The program prompts the user to enter a number, calculates the sum of its divisors, and then checks if this sum is equal to the original number.

Code Example: Perfect Number

import java.util.Scanner;

public class PerfectNumber {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int number, sum = 0;

        System.out.println("Enter a number to check for perfect: ");
        number = reader.nextInt();
        reader.close();

        for (int i = 1; i < number; i++) {
            if (number % i == 0) {
                sum = sum + i;
            }
        }

        if (sum == number) {
            System.out.print(number + " is a perfect number");
        } else {
            System.out.print(number + " is not a perfect number");
        }
    }
}

Code Explanation

Class Definition

public class PerfectNumber

The class PerfectNumber contains the main method to execute the program.

public class PerfectNumber {
    // class contents
}

Main Method

public static void main(String[] args)

The main method executes the program and contains the logic for checking if a number is perfect.

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int number, sum = 0;

    System.out.println("Enter a number to check for perfect: ");
    number = reader.nextInt();
    reader.close();

    for (int i = 1; i < number; i++) {
        if (number % i == 0) {
            sum = sum + i;
        }
    }

    if (sum == number) {
        System.out.print(number + " is a perfect number");
    } else {
        System.out.print(number + " is not a perfect number");
    }
}

Creating Scanner Object

  • Scanner reader = new Scanner(System.in); creates a Scanner object to read input from the user.

Variable Declaration

  • int number, sum = 0; declares variables to store the input number and the sum of its divisors. The sum is initialized to 0.

Prompting for User Input

  • System.out.println("Enter a number to check for perfect: "); prompts the user to enter a number.
  • number = reader.nextInt(); reads the integer input and assigns it to the variable number.
  • reader.close(); closes the Scanner object to free up resources.

Calculating the Sum of Divisors

  • for (int i = 1; i < number; i++) { starts a loop that iterates from 1 to one less than the input number.
  • if (number % i == 0) { checks if i is a divisor of number by using the modulus operator % to see if there is no remainder.
  • sum = sum + i; adds the divisor to the sum if the condition is true.

Checking if the Number is Perfect

  • if (sum == number) { checks if the sum of the divisors is equal to the original number.
  • System.out.print(number + " is a perfect number"); prints that the number is perfect if the condition is true.
  • } else { handles the case where the number is not perfect.
  • System.out.print(number + " is not a perfect number"); prints that the number is not perfect if the condition is false.

This Java code example demonstrates how to find and print all perfect numbers within a given range. A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. The program prompts the user to enter an upper bound, then iterates through all numbers up to that bound, checking each one to determine if it is a perfect number.

Code Example: Perfect Number Series

import java.util.Scanner;

public class PerfectNumberSeries {
    private static void checkPerfect(int max) {
        int number, min = 1, i, sum = 0;

        for (number = min; number <= max; number++) {
            sum = 0;
            for (i = 1; i < number; i++) {
                if (number % i == 0) {
                    sum = sum + i;
                }
            }
            if (sum == number) {
                System.out.println(number + " is a perfect number");
            }
        }
    }

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int max;

        System.out.print("Enter the upper bound to find perfect numbers: ");
        max = reader.nextInt();
        reader.close();

        checkPerfect(max);
    }
}

Detailed Code Explanation

Helper Method: checkPerfect

private static void checkPerfect(int max)

The checkPerfect method checks all numbers from 1 up to the given maximum value to see if they are perfect numbers. It prints each perfect number it finds.

private static void checkPerfect(int max) {
    int number, min = 1, i, sum = 0;

    for (number = min; number <= max; number++) {
        sum = 0;
        for (i = 1; i < number; i++) {
            if (number % i == 0) {
                sum = sum + i;
            }
        }
        if (sum == number) {
            System.out.println(number + " is a perfect number");
        }
    }
}

Initializing Variables

  • int number, min = 1, i, sum = 0; declares and initializes variables to store the current number being checked (number), the minimum value (min), a loop counter (i), and the sum of the divisors (sum).

Outer Loop: Iterating Through Numbers

  • for (number = min; number <= max; number++) { iterates through each number from 1 to the maximum value specified by the user.

Inner Loop: Calculating Sum of Divisors

  • sum = 0; initializes the sum of divisors to zero for each new number.
  • for (i = 1; i < number; i++) { iterates through all numbers less than the current number.
  • if (number % i == 0) { checks if i is a divisor of number.
  • sum = sum + i; adds the divisor to the sum.

Checking and Printing Perfect Numbers

  • if (sum == number) { checks if the sum of the divisors is equal to the current number.
  • System.out.println(number + " is a perfect number"); prints the number if it is perfect.

Main Method

public static void main(String[] args)

The main method prompts the user for input, reads the upper bound, and calls the checkPerfect method.

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int max;

    System.out.print("Enter the upper bound to find perfect numbers: ");
    max = reader.nextInt();
    reader.close();

    checkPerfect(max);
}

Creating Scanner Object

  • Scanner reader = new Scanner(System.in); creates a Scanner object to read input from the user.

Prompting for User Input

  • System.out.print("Enter the upper bound to find perfect numbers: "); prompts the user to enter the upper bound.
  • max = reader.nextInt(); reads the integer input and assigns it to the variable max.
  • reader.close(); closes the Scanner object to free up resources.

Calling the Helper Method

  • checkPerfect(max); calls the checkPerfect method with the user-provided upper bound.