Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedWhat 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions