This is a Java Program to Find if a Number is Prime or Not using Recursion. A number is said to be a prime number if it is divisible only by itself and unity.

Enter an integer as an input. Now we create a new method named prime which uses if conditons to give the desired result.

Here is the source code of the Java Program to Find if a Number is Prime or Not using Recursion. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;

public class Prime

`{`

public static void main(String[] args)

`{`

int n, x;

Scanner s = new Scanner(System.in);

System.out.print("Enter any number:");

n = s.nextInt();

Prime obj = new Prime();

x = obj.prime(n, 2);

if(x == 1)

`{`

System.out.println(n+" is prime number");

`}`

`else`

`{`

System.out.println(n+" is not prime number");

`}`

`}`

int prime(int y,int i)

`{`

if(i < y)

`{`

if(y % i != 0)

`{`

return(prime(y, ++i));

`}`

`else`

`{`

return 0;

`}`

`}`

return 1;

`}`

`}`

Output:

$ javac Prime .java $ java Prime Enter any number:17 17 is prime number

**Sanfoundry Global Education & Learning Series – 1000 Java Programs.**

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.