Write a recursive function to determine if a number is prime.

Answers

Answer 1

Answer:

Follows are the code to find the prime number:

import java.util.*;//import package for user input  

public class Main//defining a class

{  

public static boolean Prime(int x, int j)//defining a method isPrime    

   {  

       if (x<= 2) //use if block to check x less than equal to 2

           return (x== 2) ? true : false; //use bitwise operator to return value  

       if (x%j == 0) //use if to check x%j equal to 0

           return false;//return false value  

       if (j*j > x)//defining if that check i square value greater than n  

           return true; //return true value

       return Prime(x, j+1); //callling recursive method

   }  

   public static void main(String[] as)//main method  

   {  

       int n; //defining integer variable

       Scanner oxc=new Scanner(System.in);//creating Scanner class object

       n=oxc.nextInt();//input value

       if (Prime(n, 2))  //use if block to call isPrime method

           System.out.println("Yes"); //print value  

       else //else block

           System.out.println("No");  //print value  

   }  

}  

Output:

5

Yes

Explanation:

In this code, a static boolean method "Prime" is declared, that accepts two integer variables in its parameter and defines the if block that checks the prime number condition by the recursive method and returns a value true or false. Inside, the main method an integer variable is declared that uses the Scanner class object for input value and passes into the method and prints its value.


Related Questions

A system administrator at Universal Containers created a new account record type. However, sales users are unable to select the new record type when creating new account records. What is a possible reason for this? (Choose 2)

Answers

Explanation:

We have these reasons below as the

The reason why sales users are not able to select the new record type while they are trying to create a new account:

1. The users profile does not contain the record type yet. That is, this record type has not been added to the profile of the sales user.

2. This record type is yet to be activated.

Other Questions
Air pressure is greatest at the surface and decreases as you travel up through each layer and into outer spaceQuestion 4 options: True False Richard Brian puts $30 worth of gas in his truck. The price per gallon was $2.50. Which equation can be solved to find the number of gallons Richard Brian's purchased? Question 3) What's your opinion on Mr. Trump? HELP PLEASE!!!! A rock climber ascends 18 feet to the top of a rock ledge. The climberdescends 8 feet and takes a break on a rock outcrop. Write and evaluatean addition expression to show how much farther the rock climber mustdescend to reach the original level. Colin was thinking of a number. Colin halves it and gets an answer of 5.2. What was the original number? One responsibility of the Center for Drug Evaluation and Research is to work with product marketers.true or false? A loop that will output only the names that come before "Thor" in the alphabet from the names list. Hi, Can you help me out pls? A rental company charges $15 plus $4 per hour to rent a bicycle. If Marley does not want to spend more than $27 for her rental, WRITE and SOLVE an inequality (in the space below) to find how many hours she can rent the bicycle and not spend more than $27. Please show your work Which country on the map above is both part of mainland Southeast Asia and an island?A.Vietnam (A)B.Malaysia (B)C.Indonesia (C)D.the Philippines (D) How engineer can find problems and solutions Give example help plzzzzzzzzzzzzzzzzzzzzzzzzz Carlin Company, which uses net present value to analyze Investments, requires a 10% minimum rate of return. A staff assistant recently calculated a $500,000 machine's net present value to be $86,400, excluding the impact of straight-line depreciation 6.105 FV of 1 (i = 100,-5): 1.611 TV of a series of $i cash flows ( - 108, n. 5): PV of $1 (1 - 108; n = 5) 0.621 PV of a series of $1 cash flown (1 101, n = 5) 3.791 # If Carlin Ignores income taxes and the machine is expected to have a five-year service life, the correct net present value of the machine would be: a) $292,700 b) $186,400 c) $86,400 Steven Ainsley is an advocate for keeping the jury selection as it is currently in the judicial system True false what is meant by the fact that length of a object is 4 metre based on the data for the groups of cells cultured with 0.1 serum (groups 3 to 7), identify which of the four proteins tested do(does) not appear to play a role in blocking cell cycle entry by fibroblast write your opinion for against on federalism that contributes in the local development in a sentence If we play our cards right, we may be able to make it to the ball game on time.A) SimileB) Idiom C) MetaphorD) Personification which equation shows that 9 is a factor of 72 (A) 72=63+9(B) 72=81 -9(C) 8=72 / 9(D) 9=72x8 What is the formula for zinc fluoride? Can someone please help me, pick the word that does not belong