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.
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");
}
}
}
public class PerfectNumber
The class PerfectNumber
contains the main method to execute the program.
public class PerfectNumber {
// class contents
}
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");
}
}
Scanner reader = new Scanner(System.in);
creates a Scanner
object to read input from the user.int number, sum = 0;
declares variables to store the input number and the sum of its divisors. The sum is initialized to 0.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.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.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.
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);
}
}
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");
}
}
}
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
).for (number = min; number <= max; number++) {
iterates through each number from 1 to the maximum value specified by the user.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.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.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);
}
Scanner reader = new Scanner(System.in);
creates a Scanner
object to read input from the user.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.checkPerfect(max);
calls the checkPerfect
method with the user-provided upper bound.