-1

everyone, I tried this program but not get the expected answer, Please help me out.

Question is:

Print all prime numbers between two given numbers. For example: Function is Prime(beg,end), for Prime(4,7) it will return {5,7}, for Prime(7,16) it will return {7,11,13}. My code :

package com.robin.practise;

import java.util.Scanner;

public class PrimebetweenRange 
{

    private static String Prime(int beg, int end)
    {
        String res= " ";
        for(int i= beg; i<= end; i++)
        {
            for(int j=2; j<= end; j++)
            {
                if(i%j!= 0)
                {
                    //System.out.println(i+  " ");
                    res= i+ " ";
                }
            }
        }
        return res;
    }

    public static void main(String[] args) 
    {
        Scanner scn= new Scanner(System.in);
        System.out.println("Enter any two numbers: ");
        int n1= scn.nextInt();
        int n2= scn.nextInt();
        scn.close();
        System.out.println(Prime(n1, n2));
    }
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
  • Welcome to SO. Please finish the tour and you will understand [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and modify question accordingly to minimal working example. Without posting your code you risk removal of your question. With your stated trail code and error... people are more willing to help you so both can learn. Enjoy SO ;-) – ZF007 Jan 06 '18 at 12:50

5 Answers5

1

You cannot decide that a number if prime if it is not divisible by a number. You need to add to the result at the end of the inner loop.

There are quite a few problems in your code. Here's the working one

for(int i= beg; i<= end; i++) {
    boolean prime = true;  //start by assuming the current number is prime
    for(int j=2; j<i; j++) { // Loop till j < i
        if(i%j == 0) {
          prime = false; //Set the current number as not prime if it is divisible by any number lesser than it
        }
   }
   if (prime) {
       res += i+ " ";   //Add to result
   }
}

Note: As sanit@ answer says, you can terminate the loop much earlier. Refer to this to know why looping till square root of the number is enough.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0

The answers above aren't enough to solve this. At any run of the second loop, you wanna check if the i enter every if of any j. Define a boolean,for each i, check if for each j if j is dividing him, (while j isn't i itself). If so, then don't do anything, and keep running.Otherwise, concatenate i with the res so far, and print it at the end;

boolean flag=true;
for(int i= beg; i<= end; i++)
    {
        for(int j=2; j<= end; j++)
        {
            if(i%j== 0 && i!=j)
            {
                flag=true;
                res= i+ " ";
            }
        }
    if(!flag){
      res+=i +" ";}
    flag=false;
    }
    return res;
}
Or251
  • 196
  • 10
0
private static String Prime(int beg, int end) {
    String res = " ";
    outer: for (int i = beg; i <= end; i++) {
        int upto = (int) Math.sqrt(end);
        for (int j = 2; j <= upto; j++) {
            if (i != j && i % j == 0) {
                continue outer;
            }
        }
        System.out.println(i + " ");
    }
    return res;
}

I have optimized the code little bit using Math.sqrt, you can still do some more optimization, just google how to optimize prime number genearation.

sanit
  • 1,646
  • 1
  • 18
  • 21
  • 1
    Would be better if you can get rid of labels and use `break` – Thiyagu Jan 06 '18 at 13:04
  • how to check the test cases and the complexity of this each code? Actually I am new in programming and I am not familier with these terms will please tell me how to became expert in these things – Rabindra Kumar Mahato Jan 06 '18 at 16:49
0

Here is a working solution:

public static void primeBetween(int beg, int end) {
        List<Integer> result = new ArrayList<Integer>();
        for (int n = beg; n <= end; n++) {
            boolean prime = true;
            for (int j = 2; j <= n / 2; j++) {
                if (n % j == 0 && n != j) {
                    prime = false;
                }
            }
            if (prime) {
                result.add(n);
            }
        }
        System.out.println(result);
    }

    /* Driver program  */
    public static void main(String[] args) {
        primeBetween(2, 25);
    }

This is the output: [2, 3, 5, 7, 11, 13, 17, 19, 23]

sailaja.vellanki
  • 329
  • 1
  • 2
  • 8
0

This method would print the prime numbers between two Numbers

public static void getPrime(int low, int high) {

    while (low < high) {
        boolean flag = false;

        for(int i = 2; i <= low/2; ++i) {
            // condition for nonprime number
            if(low % i == 0) {
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.print(low + " ");

        ++low;
    }

}

Aaeb
  • 1,276
  • 11
  • 10