Question

A perfect number is a positive integer that is equal to the sum of its (proper)...

A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder).

For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2 + 3 = 6, the number 6 is a perfect number.

Write a Java program that reads in an integer value from the user (say val) and outputs whether this entered number is a perfect number or not. Your program should prompt and print the output as shown in examples below. First example,

Enter an integer: 6
6 is a perfect number!

Second example,

Enter an integer: 8
8 is NOT a perfect number.

Values shown in bold font in above examples are user entered values.

Hint: Steps of this program can be summarized as follows:

1) Read in the user input and store it using an integer variable (say val). (You can assume that user passed in value will be at least 2.)

2) Declare an integer variable (say divisorSum) to store the sum of the divisors. Initialize it to one.

3) User for loop as,

for(int i=2; i<val; i++)
{
 /*
  If i is a divisor of val, then update divisorSum
  such that divisorSum is assigned to the sum of 
  divisorSum (from the previous iteration) and i.
 */
}

4) If divisorSum equals to val, then display "val is a perfect number!", otherwise, display "val is NOT a perfect number"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.util.Scanner;

/**
* @author
*
*/
public class PerfectNumber {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = null;
try {

scanner = new Scanner(System.in);
// prompt to read the number
System.out.print("Enter an Integer: ");
int val = scanner.nextInt();
int divisorsSum = 1;

// repeat the loop to get sum of the divisors
for (int i = 2; i < val; i++) {

if (val % i == 0)
divisorsSum += i;
}

// check with val and sum of divisors
if (divisorsSum == val) {
System.out.println(val + " is a perfect number");

} else {

System.out.println(val + " is not a perfect number");
}

} catch (Exception e) {
// TODO: handle exception
}
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
A perfect number is a positive integer that is equal to the sum of its (proper)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A perfect number is a positive integer that equals the sum of all of its divisors...

    A perfect number is a positive integer that equals the sum of all of its divisors (including the divisor 1 but excluding the number itself). For example 6, 28 and 496 are perfect numbers because 6=1+2+3 28 1 + 2 + 4 + 7 + 14 496 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 Write a program to read a positive integer value, N, and find the smallest perfect number...

  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

  • An integer is said to be a perfect number if the sum of its divisors, including...

    An integer is said to be a perfect number if the sum of its divisors, including 1(but not the number itself), is equal to the number. For example, 6 is a perfect number because 6 = 1+2+3. A) Write a function numPerfect( number ) that returns true when the number is a perfect number, false when it is not.        B) Write a C# console program that calls the function in A) to determine and print all the perfect numbers...

  • Please Write the task in c++ Task A perfect number is an integer that is equal...

    Please Write the task in c++ Task A perfect number is an integer that is equal to the sum of its divisors (where 1 is considered a divisor). For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 is 6. Similarly, 28 is perfect because it equals 1 + 2 + 4 + 7 + 14. A quite good number is an integer whose badness—the size of the difference between the...

  • C Programming Write a C program that asks the user for a positive number n (n...

    C Programming Write a C program that asks the user for a positive number n (n ≥ 1) then, the program displays all the perfect numbers up to (including) n. A number n is said to be a perfect number if the sum of all its positive divisors (excluding itself) equals n. For example, the divisors of 6 (excluding 6 itself) are 1,2 and 3, and their sum equals 6, thus, 6 is a perfect number. Display the numbers with...

  • For C++: A perfect number has these properties 1) positive integer 2) the sum of its...

    For C++: A perfect number has these properties 1) positive integer 2) the sum of its proper divisors is the number itself 6 and 28 are perfect numbers Write two functions that return true if its value parameter is a perfect number and false if its value parameter is not a perfect numbers. The two function look like bool isPerfect(int) and void isPerfect(int, bool&) If you want more perfect number you find them on the Internet.

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • An integer number is said to be a perfect number if its factors, including 1 (but...

    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 if parameter number is a perfect number. Use this method in a test program to display all the perfect numbers between 1 and 1000. Display the factors of each perfect number to confirm that the number is indeed perfect.

  • Need help programing this in C. rinteivsors Print the proper divisors of an integer value The...

    Need help programing this in C. rinteivsors Print the proper divisors of an integer value The program should read a single integer input value, which you can assume will be positive. It should then print a single line of output with all of the proper divisors of the input value in order from least to greatest. A proper divisor d of an integer n is an integer that evenly divides n: i.e., nld is an integer For example, if the...

  • Write a Python program to print all Perfect numbers between 1 to n. (Use any loop...

    Write a Python program to print all Perfect numbers between 1 to n. (Use any loop you want) Perfect number is a positive integer which is equal to the sum of its proper positive divisors. Proper divisors of 6 are 1, 2, 3. Sum of its proper divisors = 1 + 2 + 3 = 6. Hence 6 is a perfect number. *** proper divisor means the remainder will be 0 when divided by that number. Sample Input: n =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT