-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-a-number-prime.java
36 lines (28 loc) · 954 Bytes
/
is-a-number-prime.java
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
import java.util.*;
public class Main {
// function to check input number is prime or not
public static boolean isPrime(int num) {
/* Aim:
if found any factor between 2 and its square root then it is not prime
*/
for (int fact = 2; fact <= Math.sqrt(num); fact++) {
// if num divisible by fact (means fact is factor of num)
// then num is not a prime
if (num % fact == 0) return false;
}
// if found no factor then it is a prime
return true;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
// taking the total no of inputs
int t = scn.nextInt();
for (int i = 0; i < t; i++) {
// taking all the inputs, t no of times
int inpt = scn.nextInt();
// if input i prime then print "prime" otherwise "not prime"
if (isPrime(inpt)) System.out.println("prime");
else System.out.println("not prime");
}
}
}