Nangialai Khan Nangialai Khan - 5 months ago 7
Java Question

Java Program to find prime numbers

ho to write a program in java programming language to scan two numbers and find whether the given number is prime or not


If I understood your question correct you want something like this:

public void printPrimeNumbers(int start, int max) {
    for(int i = start; i <= max; i++) { 
        boolean prime = true;
        for(int divider = 2; divider < i; divider++) {
            if(i % divider == 0)
                prime = false;
        if(prime) {
            // do something with your prime number. For example:
            System.out.println("Found prime number: " + i);

That codes runs a loop from given start int to given max int and checks each number whether its prime or not and prints it in case of it being prime.


public static void main(String[] args) {
    Scanner s = new Scanner(;
    System.out.print("Start number: ");
    int start = s.nextInt();
    System.out.print("Max number: ");
    int max = s.nextInt();
    printPrimeNumbers(start, max);

Don't forget to add "static" to the printPrimeNumbers method.