Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question from the problem (Java): Find the Perfect Number Definition A perfect n

ID: 3710354 • Letter: Q

Question

Question from the problem (Java):

Find the Perfect Number

Definition

A perfect number is a positive integer that is equal to the sum of its proper positive divisors; that is, the sum of its positive divisors, excluding the number itself. Equivalently, a perfect number is a number that is one half of the sum of all of its positive divisors.

Example

The first perfect number is 6.

The proper positive divisors for 6 are 1, 2, and 3. The sum of 1, 2, and 3 is 6.

The positive divisors for 6 are 1, 2, 3, and 6. The sum of 1, 2, 3, and 6 is 12. One half of 12 is 6.

Problem Statement

Write a Java program to find all the perfect numbers between two integers provided by the user.

Step 1

Write a method named isPerfect to determine if an integer is a perfect number. The method will accept an integer argument and return either true or false - return true if the argument is a perfect number; otherwise, return false.

In the main method, test the method using a for loop from 1 to 100 and display all the perfect numbers found.

Step 2

Refine the main method.

Prompt the user for two positive integers

Find all the perfect numbers between the two integers; swap the two integers, if necessary

Count the number of perfect numbers found

Prompt the user for another pair of positive integers

End the program if the user entered -1 and print "Thank you for using the perfect number finder!"

Sample Output

Enter a positive number (enter -1 to quit): 1000

Enter a second positive number (enter -1 to quit): 1

Below are the perfect numbers between 1 and 1000:

6

28

496

There are 3 perfect numbers.

My program works for the most part but my teacher gave me some input to improve upon it to receive 100%. Their input is:

??"In the main method, have a for loop to loop from the begin number to the end number. In each iteration, 1) pass a number to the isPerfect method, which would determine if that number is a perfect number, then, return a boolean value; 2) if the return value is true, that is the number is a perfect number, print the value and increment a counter that keeps track of how many perfect numbers are found.

The isPerfect method takes in one argument and returns a boolean value. Create a boolean variable with an initial value of false. The initial value for the total variable should be zero. The loop begins from 0 to whatever the value of the argument, determines if the argument is a perfect number. If so, set the boolean variable to true. At the end of the method, returns the boolean variable. Hope this helps! Your may resubmit."

Here is my code currently:

Explanation / Answer

Below is your code. I have to completely change your code, becuase it was not following the specification. Please add comments if you have any issues.I'll solve for sure. Please add inport statements if missing.

public class IsPerfect {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int a = 0, b = 0;

while (a != -1 && b != -1) {

// input from user for the min number value of the range

System.out.print("Enter a positive number (enter -1 to quit): ");

a = input.nextInt();

//if number is -1 then exit

if (a == -1) {

break;

}

// input from the user for the max number value of the range

System.out.print("Enter a second positive number (enter -1 to quit): ");

b = input.nextInt();

//if number is -1 then exit

if (b == -1) {

break;

}

//If first number is greater than swap

if (a > b) {

int temp = a;

a = b;

b = temp;

}

//counter variable

int total = 0;

System.out.println("Below are the perfect numbers between " + a + " and " + b + ":");

//loop to check from a to b

// checking if the number if perfect than print it

// and increment the counter.

for (int i = a; i <= b; i++) {

if (isPerfect(i)) {

System.out.println(i);

total++;

}

}

//printing the total number of perfect numbers

System.out.println("There are " + total + " perfect numbers.");

}

System.out.println("Thank you for using the perfect number finder!");

}

//function to check if number id perfect

// returns true if its perfect else false

public static boolean isPerfect(int n) {

int sumOfDivisors;

int divisor;

//if number is less than 1

// then return false

if (n < 1) {

return false;

}

sumOfDivisors = 0;

//starting from divisor = 1

//because for 0 it is DivideByZero exception

for (divisor = 1; divisor < n; divisor++) {

if (n % divisor == 0) {

sumOfDivisors = sumOfDivisors + divisor;

}

}

if (sumOfDivisors == n) {

//number is perfect

return true;

} else {

//number is not perfect

return false;

}

}

}

Output

Enter a positive number (enter -1 to quit): 1000
Enter a second positive number (enter -1 to quit): 1
Below are the perfect numbers between 1 and 1000:
6
28
496
There are 3 perfect numbers.
Enter a positive number (enter -1 to quit): 1
Enter a second positive number (enter -1 to quit): 100
Below are the perfect numbers between 1 and 100:
6
28
There are 2 perfect numbers.
Enter a positive number (enter -1 to quit): -1
Thank you for using the perfect number finder!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote