1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
creativ13 [48]
3 years ago
8

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

Computers and Technology
1 answer:
Darya [45]3 years ago
6 0

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.

You might be interested in
Write a static method named evenNumbers that accepts a string of text as a parameter. Assume that the text is a series of intege
Allisa [31]

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

// Import input/output and utility libraries

import java.io.*;

import java.util.*;

Declare class/

public class Numbers {

// Main method goes here

public static void main(String[] args) throws FileNotFoundException

{

// Read input from text file evenFile.txt; Assume file name is evenFile.txt

Scanner input = new Scanner(new File("evenFile.txt"));

// Call static method here

evenNumbers(input);

}

// Static method evenNumbers begins here

public static void evenNumbers(Scanner input)

{

// Declare and initialise Variables

String Nums = input.nextLine(); // Read all number in txt file

Scanner numbers = new Scanner (Nums);

int count = 0; // Number count

int evens = 0; // Even count

int sum = 0; // Summation of numbers

// Check if number is integer. If yes, continue the below operations

while (numbers.hasNextInt())

{

int num = numbers.nextInt();

count++;

sum = sum + num;

if(num % 2 == 0) // check for even

{

evens++;

}

}

System.out.println("Total numbers= " + count); // Print total number

System.out.println("Sum of numbers = " + sum); // Print summation of numbers

System.out.println("Total evens = " + evens); // Print total even

//Calculate and print percentage of even number

double percent = ((double)evens/count)*100.0);

System.out.println("Percent of even number = " +percent);

}

}

// End of program

8 0
3 years ago
Read 2 more answers
3. Program to find the sum of all even numbers from m to n, where m and n
Gnom [1K]

Answer: M and E

Explanation: Hope this helps

8 0
3 years ago
What type of database contains multiple collections of data that are related to one another cells
statuscvo [17]
The different types<span> of </span>databases<span> include operational </span>databases<span>, end-user </span>databases<span>, distributed </span>databases<span>, analytical </span>databases<span>, relational </span>databases<span>, hierarchical </span>databases<span> and </span>database<span> models. 

Source Bing</span>
7 0
3 years ago
Which Of these best describes what a binary digit can store?
Sholpan [36]

Answer:

A

Explanation:

binary is in the root of 2 so it's only having values 1 and 0

8 0
3 years ago
Explain why it is important to always plug your computer into a surge protector.
Feliz [49]
A surge protector is designed to protect electrical devices from voltage spikes. It limits the voltage supplied by blocking or shorting to ground any unwanted voltages above a safe threshold.  
3 0
3 years ago
Other questions:
  • How to find no of Distinct triangles in a polygon?
    10·1 answer
  • Dillion’s company has decided it wants to find a way to do a better job of tracking clothing inventories. Dillon has been asked
    5·1 answer
  • How do i end my current plan that i never signed up for, the basic one it charged me $24
    11·2 answers
  • "Smart-Toys-Smart Kids" is a toy manufacturing company. They are providing toys to more than 100 different Toy retail stores in
    8·1 answer
  • How does is make you feel when you're kind to others? What are some opportunities in your life to be more kind to your friends a
    9·1 answer
  • on the road you are traveling,the pavement markings change from broken white lines to solid white lines between the lanes of tra
    5·1 answer
  • A program reads three integer values. The three values are interpreted as representing the lengths of the sides of a triangle. T
    7·1 answer
  • Columns, margins and orientation can all be found on what tab?
    15·1 answer
  • What is Accenture's role in Multi-party Systems?
    12·1 answer
  • Does the following code return a string or a number def of() return 3
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!