-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursiveAsal.java
More file actions
41 lines (38 loc) · 1.25 KB
/
recursiveAsal.java
File metadata and controls
41 lines (38 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class recursiveAsal {
static boolean isPrime(int n, int i){
// 0 and 1 are not prime numbers.
if(n==0 || n==1){
return false;
}
// 2 is prime number.
if(n==2){
return true;
}
else{
// If the number has a divisor, it must be equal to 1 and itself. Otherwise the number is not prime.
if(n%i==0){
if(i==1){
return isPrime(n,i+1);
}else if(n==i){
return true;
}else{
return false;
}
} else{
return isPrime(n,i+1);
}
}
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int n;
System.out.print("Enter number : ");
n=inp.nextInt();
if(isPrime(n,1)){
System.out.println(n + " is prime number.");
}else {
System.out.println(n + " is not prime number.");
}
}
}