forked from hackerisactivenow/hacktober-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime.java
More file actions
39 lines (37 loc) · 887 Bytes
/
prime.java
File metadata and controls
39 lines (37 loc) · 887 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
37
38
import java.util.*;
public class Prime{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("All prime numbers between "+a+" and "+b+" are: ");
if(a<b){
for(int i=a;i<=b;i++){
if(isPrime(i)==1){
System.out.println(i);
}
}
}
}
public static int isPrime(int n){
int flag=0;
if(n==2 || n==3){
return 1;
}
else{
for(int i=2;i<=n/2;i++){
if(n%i==0){
flag=1;
break;
}
}
if(flag==0){
return 1;
}
else{
return 0;
}
}
}
}