+ 3

Prime number

how to find prime number in java? using scanner class.

25th Nov 2017, 12:59 PM
sal sal
sal sal - avatar
5 Answers
+ 11
Scanner class is used to get input. Do you mean "getting input using Scanner and check if it's prime"?
25th Nov 2017, 1:36 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 11
Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(), f; for (f = 2; f < n; f++) if (n % f == 0) break; if (f == n) System.out.println("Prime");
25th Nov 2017, 4:51 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 2
yes. getting input using scanner. then finding prime number.
25th Nov 2017, 4:18 PM
sal sal
sal sal - avatar
+ 1
To find prime numbet, it's an algorithm. find the middle -> int(x/2) a for loop from 2 to x/2: check if x/num == int(x/num) - yes(1 of them) - not prime. you can ask for input you can go over a range
25th Nov 2017, 1:43 PM
Amir Galanty
Amir Galanty - avatar
0
There is no reason to check above n/2. Take the number 13... you don't have to test higher then int(13/2). The higher the number, you will test more useless numbers. https://en.m.wikipedia.org/wiki/Largest_known_prime_number
25th Nov 2017, 7:23 PM
Amir Galanty
Amir Galanty - avatar