+ 2

How to check that the nuber contains a prime number in a integer input? Java

using scanner class

10th Oct 2017, 5:44 PM
Farhan Raza
Farhan Raza - avatar
3 Réponses
10th Oct 2017, 6:47 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
If n is divisible by any number from 2 to sqrt(n), where n is the number inputted by the user and n != 2 (2 is prime). Then n is a prime.
10th Oct 2017, 6:40 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Prime number, is number that could be divided only by 1 and by it self. So, i would use a for, and divide number by increasing I starting from 1, if I divides with out lefting(Eg 5%5=0) count it. And if count equal to 2, it is a prime number.
10th Oct 2017, 6:04 PM
Abylaikhan
Abylaikhan - avatar