forked from Shreerang4/learning-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.java
More file actions
64 lines (58 loc) · 1.79 KB
/
primes.java
File metadata and controls
64 lines (58 loc) · 1.79 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// import java.util.*;
// public class primes {
// static int prime(int n){
// if(n==1) return 0;
// for(int i = 2; i<=Math.sqrt(n); i++){
// if (n%i==0){
// return 0;
// }
// }
// return 1;
// }
// public static void main(String[] args){
// try (Scanner sc = new Scanner(System.in)) {
// System.out.print("Enter A: ");
// int a = sc.nextInt();
// System.out.print("Enter B: ");
// int b = sc.nextInt();
// System.out.println("Primes: ");
// for(int i = a; i<=b; i++){
// if (prime(i)!=0){
// System.out.println(i);
// }
// }
// }catch(ArithmeticException e){
// System.out.println("Arithmetic Error! ");
// }
// }
// }
import java.util.*;
public class primes {
static int prime(int n){
if(n==1) return 0;
for(int i = 2; i<=Math.sqrt(n); i++){
if (n%i==0){
return 0;
}
}
return 1;
}
public static void main(String[] args){
try (Scanner s = new Scanner(System.in)) {
System.out.print("Enter a list of numbers: ");
String[] arr = s.nextLine().strip().split(",");
int[] nums = new int[arr.length];
for(int i = 0; i<arr.length; i++){
nums[i] = Integer.parseInt(arr[i].strip());
}
System.out.println("Primes: ");
for(int a: nums){
if (prime(a)!=0){
System.out.println(a);
}
}
}catch(ArithmeticException e){
System.out.println("Arithmetic Error! ");
}
}
}