Question: What is the java code for this Prime Note that an integer > 2 is prime if no integer between 2 and itself completely divides

What is the java code for this Prime Note that an integerWhat is the java code for this

Prime Note that an integer > 2 is prime if no integer between 2 and itself completely divides it. An integer > 2 is composite otherwise. Let us see some execution examples first, to get the sense of how the program works. 1 % java Prime 2 Enter n: 256 3 The number is a square. 4% java Prime Enter n: 137 6 The number is prime. 7% java Prime 345 8 Enter n: 345 9 The number is composite. Again, the blue texts are user inputs. The program receives an integer n from the user and then calls a void method test with n as the actual parameter. The method test first computes the integer part of Math.sqrt of n and stores it in an integer variable limit. It then checks whether limit * limit == n. If the conditional evaluation returns true, n is a square, and so the method reports the discovery and executes return;. The return statement terminates the execution of the method immediately. Since the method has type void, return with no argument works. Then the program executes a for-loop with which it iterates the value of an integer i from 2 to limit. In the loop-body, the method tests whether n % limit == 0}. If the test returns ture, the method reports that n is composite and returns. Otherwise, the loop completes. After termination of the loop, the method reports that the number is prime

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!