forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeRecursion.java
More file actions
36 lines (33 loc) · 777 Bytes
/
PrimeRecursion.java
File metadata and controls
36 lines (33 loc) · 777 Bytes
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
// check whether a number is Prime or not with recursion
import java.util.Scanner;
class PrimeRecursion
{
public static int prime(int n,int div)
{
if(div<n)
{ if(n%div!=0)
prime(n,div+1);
else
return 0;
}
return 1;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number : ");
int n=(int)Math.abs(sc.nextInt());
int c=prime(n,2);
System.out.println(n+((c!=0)?" is a Prime Number." :" is not a Prime Number."));
}
}
// Contributed By ErzaTitani-2001
/*
Sample Input and Output :
Input :
Enter a number : 13
Output :
13 is a Prime Number.
Space Complexity : O(n)
Time Complexity : O(n)
*/