+ 3
Prime number
how to find prime number in java? using scanner class.
5 Answers
+ 11
Scanner class is used to get input. Do you mean "getting input using Scanner and check if it's prime"?
+ 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");
+ 2
yes. getting input using scanner. then finding prime number.
+ 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
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