create a Java recursive method that determines if a number is a prime number

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Modularization Techniques
Section: Chapter Questions
Problem 13RQ
icon
Related questions
Question

help create a Java recursive method that determines if a number is a prime number

Do the following:
Create a recursive method computePrime(int x, int y) that returns a boolean value.
Returns true if x is prime, else returns false.
y is current divisor to check.
Ask the user to enter a number to check to see if it is a prime.
First call from main, set y equal to 2. Example: primeno = computePrime(number, 2);
Base cases
• if x < 2 return false
• if x == 2 return true
• if x % y == 0 return false
if y* y> x return true
// Otherwise, Check for next divisor
• return computePrime(x, y + 1)
Demonstrate the method in a program. (Same or separate program.)
Test the program and make sure it runs.
Transcribed Image Text:Do the following: Create a recursive method computePrime(int x, int y) that returns a boolean value. Returns true if x is prime, else returns false. y is current divisor to check. Ask the user to enter a number to check to see if it is a prime. First call from main, set y equal to 2. Example: primeno = computePrime(number, 2); Base cases • if x < 2 return false • if x == 2 return true • if x % y == 0 return false if y* y> x return true // Otherwise, Check for next divisor • return computePrime(x, y + 1) Demonstrate the method in a program. (Same or separate program.) Test the program and make sure it runs.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage