19.8k views
2 votes
JAVA (Perfect Numbers) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method isPerfect that determines whether parameter number is a perfect number. Use this method in an application that displays all the perfect numbers between 1 and and integer entered by the user. Display the factors of each perfect number to confirm that the number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger than 1000. Display the results.

This is what I have so far but I keep getting errors for the user input.

import java.util.Scanner;
public class PerfectNumbers {
Scanner input = new Scanner(System.in);
public static void main(String[] args) {

System.out.print("Enter the number up to which you would like to look for perfect numbers:");
int user = input.nextInt();
for (int num = 1; num <= user; num++) {
if (isPerfect(num)) {
System.out.println("The number " + num + " is a perfect number");
System.out.println("It's factors are:" + isPerfect(num));
}
}

}
public static Boolean isPerfect(int num) {

User Vik Gamov
by
8.3k points

1 Answer

6 votes

Final answer:

The student question pertains to writing a Java application that identifies perfect numbers. The given code has an issue with the input being incorrectly scoped. The corrected code includes a static declaration for the input scanner and a complete isPerfect function that properly checks if a number is perfect.

Step-by-step explanation:

Perfect Numbers in Java

The concept of perfect numbers is a mathematical concept that lies within the field of number theory, but the student's question relates to writing a Java program that will identify such numbers, thus transitioning it to the realm of computers and technology. In your provided Java code, there seems to be an issue with the scope of the variable 'input'. The input object is declared outside of the main method without being marked as static, which is why you are encountering errors. Moreover, your isPerfect method is not complete and therefore cannot determine if a number is a perfect number. Below is the corrected code.

import java.util.Scanner;
public class PerfectNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number up to which you would like to look for perfect numbers:");
int user = input.nextInt();
for (int num = 1; num <= user; num++) {
if (isPerfect(num)) {
System.out.println("The number " + num + " is a perfect number");
// You also need to create a method to display factors
}
}
}

public static boolean isPerfect(int num) {
int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum == num;
}
}

The isPerfect method checks all numbers less than the entered number to see if they are factors, and if they are, it adds them to a running sum. If the sum of these factors equals the original number, it returns true, indicating that the number is indeed perfect.

User Nilanka Manoj
by
8.2k points

No related questions found