Question

JustPerfects.java (for Loop). In this program, you will use loops to identify perfect numbers (a number...

JustPerfects.java (for Loop).

In this program, you will use loops to identify perfect numbers (a number equal to the sum of its divisors - 6 and 28 are the first two! 6 is 2 +3 + 1 and 28 is 14+7+4+2+1). Being the perfect number is quite unique. Only 4 perfect numbers exist between 1 and 10,000 and you’ve already found two! Please write a program to find the additional pair.

Bonus: How many perfect numbers exist between 10,000 and 35,000? (+2)

Homework Answers

Answer #1
public class JustPerfects {

    public static void main(String[] args) {
        for (int i = 1; i <= 10000; i++) {
            int sum = 0;
            for (int j = 1; j < i; j++) {
                if (i % j == 0)
                    sum += j;
            }
            if (sum == i) {
                System.out.println(i);
            }
        }
        int count = 0;
        for (int i = 10000; i <= 35000; i++) {
            int sum = 0;
            for (int j = 1; j < i; j++) {
                if (i % j == 0)
                    sum += j;
            }
            if (sum == i) {
                ++count;
            }
        }
        System.out.println("Number of perfect numbers between 10,000 and 35,000 is " + count);
    }
}

Know the answer?
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
Similar Questions
An integer number is said to be a perfect number if it is equal to the...
An integer number is said to be a perfect number if it is equal to the sum of its factors (divisors), including 1 (but not the number itself). For example, 6 is a perfect number because 6 = 3+2+1. Write a method called isPerfect that returns true if the input integer number is a perfect number and false otherwise. Then, call this method in the main method that determines and prints all the perfect numbers between 2 and 2000. WRITE...
Write an Assembley Language.A pair of numbers m and n are called "Perfect Dancing Partners" if...
Write an Assembley Language.A pair of numbers m and n are called "Perfect Dancing Partners" if the sum of all divisors of m (excluding m) is equal to the number n and the sum of all divisors of n (excluding n) is equal to m (with m ≠ n). For example, the numbers 220 and 284 are perfect dancing partners because the only numbers that divide evenly into 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55,...
Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 +...
Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 + 125 + 27 = 153. Namely, 153 is equal to the sum of the cubes of its own digits. Perfect Numbers: A number is said to be perfect if it is the sum of its own divisors (excluding itself). For example, 6 is perfect since 1, 2, and 3 divide evenly into 6 and 1+ 2 + 3 = 6. Input File The input...
Assembler lanaguage program   The program is to prompt the user to enter a number between 2...
Assembler lanaguage program   The program is to prompt the user to enter a number between 2 and 100. Reject any invalid user inputs and terminate the program if invalid inputs are entered. Print each even number from 2 to the user entered number. Sum these even numbers and print the sum. Print each odd number from 1 to the user entered number. Sum these odd numbers and print the sum. Exit the program when the output is complete. The output...
use python Write a program that uses a for loop to display all prime numbers within...
use python Write a program that uses a for loop to display all prime numbers within the range [500, 800] (hint: prime numbers are numbers that have only 2 factors: 1 and themselves. You can use a loop function to check if a number has a factor other than 1 or itself using % operation)
MATLAB Task: Use a for loop to find how many numbers you have to add to...
MATLAB Task: Use a for loop to find how many numbers you have to add to get a sum greater than 1,000. The numbers in the problem are consecutive, so adding up 1+2+3+4+5+6+.... until you reach the user defined value 1000. Method : must Make a variable equal to 1 then add 2, 3, 4...to it, until you reach 1000, then you break out the loop once the sum hits 1,000.
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Simulate 1000 rolls of a pair of dice. (Hint: Sounds like a For Loop to me!)...
Simulate 1000 rolls of a pair of dice. (Hint: Sounds like a For Loop to me!) Write a program that simulates a dice roll by picking a random number from 1-6 and then picking a second random number from 1-6.    How many times do you get two 1’s. What many times do you get two 1’s if you simulate 10,000 rolls of a pair of dice? Upload your program and type the answer to the two questions. (remember, these...
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...