0

I have to find the next prime number after a given number, but I stuck. Can you help me with for loop? I think there's a problem. I tried to find on the internet but I need the simplest possible solution :(

 public static boolean prime(int num) {
    for (int i = 2; i < Math.sqrt(num); i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;

}

public static void main(String[] args) {
    int a = 7;
    a += 1;
    for (int i =a; i < 100; i++) {

        if (prime(i)) {
            System.out.println("the next prime number is "+i);
            break;
        }
    }
  • Possible duplicate of [Optimal way to find next prime number (Java)](https://stackoverflow.com/questions/47407251/optimal-way-to-find-next-prime-number-java) – R. Daumann Jul 30 '18 at 10:37
  • @R.Daumann Not helpful.. –  Jul 30 '18 at 10:45
  • What is the problem? Where exactly are you stuck? Note that it should be i <= Math.sqrt(num), otherwise you are returning true for numbers such as 4. Also prime(1) returns true – juvian Jul 30 '18 at 16:18

0 Answers0