In this article we are going to see how we can check if a number is prime or not using recursion by Java programming language.
Java Program to Check If a Number is Prime or Not Using Recursion
A number is said to be prime number if it has only 2 factors i.e. 1 and the number itself. Means other than 1 and itself it is not divisible by any other numbers.
For example- 7, 13, 19, 29 etc.
Here you have to write the program check if a number is prime or not using recursion.
- Java Program to Check If a Number is Prime or Not Using Recursion By Using Static Input Value
- Java Program to Check If a Number is Prime or Not Using Recursion By Using User Input Value
Method-1: Java Program to Check If a Number is Prime or Not Using Recursion By Using Static Input Value
Approach:
- Declare an Integer variable ‘
num’ and initialize with 59. - Call the user defined method
checkPrime( )by passing the ‘num’ and its half. - The user-defined method takes a ‘
num’ and its half as input. Then keeps on decrementing the half until it divides the ‘num‘ or it reaches 1 using recursion. - Print the result.
Program:
import java.util.*;
// Main class
public class Main
{
// Recursive method to check for prime
public static int checkPrime(int num, int i)
{
if (i != 1)
{
// checks if the number is divisible
if (num % i != 0)
{
// Decrements the divisor by 1 every call
return checkPrime(num, i - 1);
}
else
{
return 0;
}
}
else
{
return 1;
}
}
public static void main(String[] args)
{
int num = 59;
// Call the method and store the result
int res = checkPrime(num,num/2);
// Print the result
if(res==1)
System.out.println(num+" is prime.");
else
System.out.println(num+" is not prime.");
}
}
Output: 59 is prime.
Method-2: Java Program to Check If a Number is Prime or Not Using Recursion By Using User Input Value
Approach:
- Ask the user to enter a number and store it in ‘
num‘ variable. - Call the user defined method
checkPrime( )by passing the ‘num‘ and its half. - The user-defined method takes a ‘
num‘ and its half as input . Then keeps on decrementing the half until it divides the ‘num‘ or it reaches 1 using recursion. - Print the result.
Program:
import java.util.*;
// Main class
public class Main
{
// Recursive method to check for prime
public static int checkPrime(int num, int i)
{
if (i != 1)
{
// checks if the number is divisible
if (num % i != 0)
{
// Decrements the divisor by 1 every call
return checkPrime(num, i - 1);
}
else
{
return 0;
}
}
else
{
return 1;
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// Asks the user for input
System.out.println("Enter a number");
int num = sc.nextInt();
// Call the method and store the result
int res = checkPrime(num,num/2);
// Print the result
if(res==1)
System.out.println(num+" is prime.");
else
System.out.println(num+" is not prime.");
}
}
Output: Enter a number 153 153 is not prime.
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.