Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you please help fix my code I am getting this error java.lang.ArithmeticExce

ID: 3887355 • Letter: C

Question

Can you please help fix my code I am getting this error

java.lang.ArithmeticException

* Use RECURSION to write this function (or make this function a wrapper over another recursive function)

* a number is prime if it is divisible only by itself and 1 (that is, if it is not divisible by any number between * itself and 1; we can optimize and just check between 2 and the square root of the number).

* by convention, 1 is NOT prime

* this function returns true if its parameter is prime, false otherwise.

* One way to do this is to test all numbers between 2 and n; if any of them divides it, then it is not prime. If you reach the end, then it is.

* Examples:

* isPrime(1) => false

* isPrime(2) => true

* isPrime(3) => true

* isPrime(4) => false

*/

public static boolean isPrime(int n)

{

return isPrime(n, n-1);

}

Explanation / Answer

import java.util.Scanner;

public class isPrime {

   public static boolean isPrime(int n)

   {

      return isPrime(n, n-1);

   }

   private static boolean isPrime(int n, int i)

   {

       if(i==1)

       {

           return true;

       }

       if(n%i==0)

       {

           return false;

       }

       return isPrime(n,i-1);

   }

   public static void main(String[] args)

   {

       Scanner scan = new Scanner(System.in);

       int n = scan.nextInt();

       System.out.println(isPrime(n));

   }

}