-1
public class PrimeNumbers {
    public static void main(String[] args) {
        int num = 6;
        boolean isPrime = true;

        for (int i = 2; i <= num - i; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
        int nextPrime = num++;
        {
            while (isPrime = false) {


            }
            if (isPrime)
                System.out.println("Is a prime number");
            else
                System.out.println("Is not a prime number" + "the next prime number is" + nextPrime(num));
        }
    }
}

I have tried to code in Java in order to find the next prime number while the previously returned is false, but I am really confused about how to proceed with "while" code. This is what I have tried until now:

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Maria
  • 47
  • 1
  • 7
  • 3
    Does this answer your question? [Program to find prime numbers](https://stackoverflow.com/questions/1510124/program-to-find-prime-numbers) – Yoshikage Kira Nov 11 '20 at 17:26

3 Answers3

0

In order to find a prime number, you basically need a nested loop. So I'd recomend having your for loop INSIDE the while loop. OR you can have a do-while loop on the outside that keeps looping until you found a prime number.

Your code should look like this:

public class PrimeNumbers {

    public static boolean check_prime(int num){
        
        for (int i = 2; i <= num-i ; i++) { 
            if (num % i == 0) {             
                return false;
            }
        } 
        return true;
    }

    public static void main(String[] args){
        int num = 6;

        boolean isPrime= true;

        if(!check_prime(6)){                        
            int nextPrime = num;
            do{
                nextPrime++;
                if (check_prime(nextPrime)) {             
                    isPrime = true;
                    break;
                 }else{
                    isPrime = false;
                 }
            }while(!isPrime);
        }
    
        if (isPrime)
            System.out.println("Is a prime number");
        else
            System.out.println("Is not a prime number" +"the next prime number is" + nextPrime);
    }}}
  • Hello! Thanks a lot! What I am trying to define is another variable that tells me what the next prime number is after asking the program whether a number is prime or not. So the code should be: ask whether a given number is prime or not. In case yes, print. In case no, print no and indicate the next prime number. I am having trouble to define the variable of the next prime number. – Maria Nov 11 '20 at 17:32
  • Okay I see, I have edited my code above, hopefully it helps! – Segmentation Fault Nov 11 '20 at 17:53
0

You can create two functions: one (say, isPrime) to check if the number is prime or not and the second (say, nextPrime) to find the next prime. The logic in the second function will be quite straight forward: keep incrementing the number by 1 until isPrime returns true.

Note that you need to check only up to the square root of the number in order to determine if it is prime. Check https://en.wikipedia.org/wiki/Primality_test to learn more about it.

Demo:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int n = scanner.nextInt();
        if (isPrime(n)) {
            System.out.println("It is a prime number");
        } else {
            System.out.println("It is not a prime number. The next prime number is " + nextPrime(n));
        }
    }

    static boolean isPrime(int n) {
        if (n == 1 || n == 0) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    static int nextPrime(int n) {
        while (!isPrime(n)) {
            n++;
        }
        return n;
    }
}

A sample run:

Enter an integer: 10
It is not a prime number. The next prime number is 11

Another sample run:

Enter an integer: 11
It is a prime nmber
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thanks a lot! I want to ask you a question regarding this code, as I am a beginner: regarding the nextPrime part, why do we code n++; } return n; – Maria Nov 11 '20 at 17:48
  • @Maria - Programming is all about thinking as if you are doing Maths. Think about what you would do if you are given a number, `n` and asked to find the next prime number. You will first check if it is prime or not and if it turns out to be non-prime, you will try the next number (which `n++`). The operation, `n++` means `n = n + 1`. If the next number also turns out to be non-prime, you will increment it by `1` and repeat the process until you arrive at a prime number. Feel free to comment in case you have any further doubt. – Arvind Kumar Avinash Nov 11 '20 at 17:52
0

How about this snippet?

public static void main(String[] args) {
    int number= 14; // will print the second SOUT statement with 17
    if (isPrime(number)) {
        System.out.println(number+ " is a prime number");
    } else {
        System.out.println(number+ " is not a prime number, " + 
                "the next prime number is " + nextPrime(number));
    }
}
/**
 *  Check if the number is a prime number
 *  For 0 or 1 you can return false, for the rest if is divisible by each number
 *  You might want to throw an exception for the negative input 
 */
static boolean isPrime(int number) {
    if (number==0 || number==1) {
        return false;
    }
    for (int i=2; i<=number/2; i++) {
        if (number%i==0) {
            return false;
        }
    }
    return true;
}
/**
 *  Iterate the number by one until you find a prime number using the while cycle
 */
static int nextPrime(int number) {
    int prime;
    while (true) {
        boolean isPrime = isPrime(++number);
        if (isPrime) {
            prime = number;
            break;
        }
    }
    return prime;
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183