Question: This code is to print the series of prime number up to given limit but when I am trying to execute this,it goes into infinite loop.

```
import java.io.*;
class a
{
    public static void main(String s[]) throws IOException
    {
        int count=1;
        String st;
        System.out.println("how many prime no. do you want");
        BufferedReader obj= new BufferedReader (new InputStreamReader (System.in));
        st=obj.readLine();
        int n=Integer.parseInt(st);
        while(count!=n)
        {
            int num=2;
            for(int i=2;i<num;i++)
            {
                if(num%i==0)
                {
                    count++;
                    break;
                }
            }
            num++;
        }
    }
}

```

Answer: 
USe isPrime(int num) method in loop to generate prime numbers example is show below code 

```
public class PrimeNumbersSeries 
{
    public boolean isPrime(int num)
    {
        boolean flag=true;
        for(int i=2; i<=num/2; i++)
        {
            if(num%i==0)
            {
                flag=false;
            }
            else 
           {
               flag=true;
           }
        }
        if(num<=1)
        {
            flag=false;
        }
        return flag;
    }
    public static void main(String []args)
    {
        System.out.println("how many prime no. do you want");
        PrimeNumbersSeries prime=new PrimeNumbersSeries();
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();
        int count =0;
        int j=1;
        while(count!=num)
        {
            if(prime.isPrime(j))
            {
                System.out.print(j+", ");
                count++;
            }            
            j++;
       }
   }
}

```
You can take a look at this code. 

```
import java.io.*;
public class Main {

     public static void main(String[] args) throws IOException {

        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        //How many prime numbers you want to print?
        String s = bf.readLine();
        int n = Integer.parseInt(s);
        int count = 0;
        boolean loop = true;
        for(int i =2 ; loop ; i++){

            if(isPrime(i))
                {
                System.out.println(i + " ");
                count++;
                }
            if(count == n)
                loop = false;
        }
    }

```

The following isPrime() method checks whether a number is prime or not. If the number is prime, it returns true, otherwise false.

```
    public static boolean isPrime(int num) {
        boolean prime = true;
        for(int i=2 ; i<= Math.sqrt(num);){
            if(num % i ==  0)
                {
                    prime = false;
                    break;
                }
            if(i >= 3)
            /* when i>=3 we do not need to check for every number. 
              For avoiding even numbers i is incremented by 2. 
              It reduces the number of looping */ 
                i+=2;
            else
                i++;
        }

        return prime;

    }
}

```