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) {

Respuesta :

Answer:

The answer to this question can be given as:

Program:

import java.util.*;  //import package.

public class  PerfectNumbers //define class.

{

public static void main(String[] args)  //define main method  

 {

System.out.print("Enter the number up to which you would like to look for perfect numbers:"); //message

Scanner input = new Scanner(System.in);  //creating scanner class Object

 int user = input.nextInt();  //input from user.

   for (int num = 1; num <= user; num++) //loop.  

   {

     if (isPerfect(num))   //condittional statement.

     {

       System.out.println("The number " + num + " is perfectnumber");

       System.out.println("It's factors are:" + isPerfect(num));

    }

   }

 }  

 public static Boolean isPerfect(int num)  //define function isPerfect.

 {

   int sum = 0,n;  //define variable.

n=num;

  for (int factor = 1; factor < n; factor++)  /loop.  

  {

     if (n % factor == 0)  

//check conditions n modules factor is equal to 0.

     {

       sum =sum+ factor;  //addition of factor.

     }

   }

   if (sum == n)   //check sum is equal to n.

   {

     return true;       //return values.

   }  

   else

   {

     return false;

  //return values.

   }

 }  

}

Output:

Enter the number up to which you would like to look for perfect numbers:12

The number 6 is perfect number

It's factors are:true

Explanation:

In the above program first, We import the java util package form user-input. Then we declare the class PerfectNumbers that is given in the question. Then we declare the main method in the main method we create scanner class object and declare an integer variable user that takes an input from the user we declare the for loop. In the loop, we use the conditional statement in the if block we print all the values of the isPerfect() function. Then we declare isPerfect() function this function return type is Boolean that means it returns only true or false. In this function, we pass a parameter that prints the factor of the number. In this function, we check if number and sum are equal then it returns true else it returns false.