Question

Can you answer this question in Java and explain your process? Write a program that generates...

Can you answer this question in Java and explain your process?

Write a program that generates random numbers between 1 and 100 until the random number is a perfect number. You must compute all the divisors.

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

Ans :

Steps to solve the problem:

  1. use while loop and give parameter as true untill random number is generted is a perfeect number
  2. inside while loop generate random number at each iteration
  3. create a function with name checkPerfect(int n) that will check that a number is perfect number or not. for checking that a number is prime or not simply iterate the loop from 1 to n-1 and check it is a divisor if it is then simply add it to the sum if sum is equal to the number than it is a perfect number and return true else return false.
  4. if random number generated is perfect then simply break out from the while loop.

Java code :

/* importing random class to generate random number*/
import java.util.Random;
public class Perfect {


    //creating function to check random number is perfect or not
    public static boolean checkPerfect(int n)
    {
        //declaring sum variable
        int sum=0;


        //iterating loop till n-1 to check prime divisor
        for (int i=1;i<n-1;i++)
        {
            //if i is prime divisor the add it to the sum
            if (n % i==0)
            {
                sum = sum + i;
            }
        }
/* //checking that number is equal to the sum of all its divisor if yes return true
* else return false*/

        if (sum == n && n != 1)
            return true;

        return false;
    }

    public static void main(String[] args) {

        //creating object of random number class
        Random random=new Random();

        //executing loop till condtion is true
        while(true)
        {
            //generating random number
            int i=random.nextInt(100);

            //function call for checkPerfect function
            if(checkPerfect(i))
            {
                //if it perfect number simply break out from the loop
                System.out.println("Random number "+i +" is  a perfect number");
                break;
            }
            else

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




    }

}

output images :

→ If Random number 62 is not a perfect number Random number 43 is not a perfect number Random number 25 is not a perfect numb

code image :

Activities IntelliJ IDEA Community Edition Thu 02:13: core-java (-/IdeaProjects/core-java] -.../src/Perfect.java File Edit Vi

Activities IntelliJ IDEA Community Edition Thu 02:13: core-java (-/IdeaProjects/core-java] -.../src/Perfect.java File Edit Vi

Add a comment
Know the answer?
Add Answer to:
Can you answer this question in Java and explain your process? Write a program that generates...
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
  • Write a program that asks the user for a lower limit and an upper limit. The...

    Write a program that asks the user for a lower limit and an upper limit. The program finds all pairs of amicable numbers where the first number of the pair is between those limits. The second number of the pair may or may not be between the limits. To do this sensibly, write a function int sumDivisors( int num ); that returns the sum of all the proper divisors of num. The main program looks at each integer N between...

  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

  • /*Write a Java program that generates 1000 random integers, * calculates the average and prints two...

    /*Write a Java program that generates 1000 random integers, * calculates the average and prints two counts, one for the * numbers lower than the average and one for the numbers larger than the average. */ Use java thanks

  • 32) Write a valid Java statement that generates a random value between 1 and 100, inclusive,...

    32) Write a valid Java statement that generates a random value between 1 and 100, inclusive, and assigns that value to randPer. 33) What library must be imported to use the Java random number generator class and its methods? Give the correct import statement needed to ensure that this class is available for use in the program using it. 34) Write a valid method in Java that performs a simulated coin-toss – meaning it returns boolean value of either true...

  • please help me with, Write a program in java that generates 10 random doubles, all between...

    please help me with, Write a program in java that generates 10 random doubles, all between 1 and 11, and writes them to a text file, one number per line. Then write another program that reads the text file and displays all the doubles and their sum accurate to two decimal places. SAMPLE OUTPUT 10.6269119604172 2.737790338909455 5.427925738865128 1.3742058065472509 1.1858700262498836 4.180391276485228 4.910969998930675 5.710858234343958 7.790857007373052 3.1806714736219543 The total is 47.13

  • Java language Part IV. Write programs (15 pts each) rite a program that generates a random...

    Java language Part IV. Write programs (15 pts each) rite a program that generates a random question of adding or subtracting two integers number and number2 under 20 (numbernumber2 in the case of subtraction) and prompt the user to enter the answer. After the user types the answer in the console, the program displays a message to indicate whether the answer is correct and gives the solution if the answer is wrong. Two sample runs are shown below. Tip: generat...

  • In JAVA, write a program that generates 10 random numbers between 0 and 9. Use count...

    In JAVA, write a program that generates 10 random numbers between 0 and 9. Use count arrays for each number. A sample output would be: The 10 random numbers are: 5 7 1 9 0 0 1 2 3 4 0 occurs 2 times 1 occurs 2 times 2 occurs 1 time 3 occurs 1 time 4 occurs 1 time 5 occurs 1 time 6 occurs 0 times 7 occurs 1 time 8 occurs 0 times 9 occurs 1 time

  • A. Create a java program that generates 1000 random integers. Write the 1000 random integers to...

    A. Create a java program that generates 1000 random integers. Write the 1000 random integers to a file using the Formatter class. B. Create a second java program that opens the file with the 1000 integers using the Scanner class. Read in the integers and find and print out the largest, smallest and the average of all 1000 numbers. C. Repeat A from above but use the BufferedWriter class B. Repeat B from above but use the BufferedReader class.

  • Can you help me write a Python 3.7 code for this question? Write a program using...

    Can you help me write a Python 3.7 code for this question? Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list 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