Program to find next five prime numbers from a given number


//            Program to find next 5 prime numbers after a given number

import java.util.Scanner;

public class next5prime
{
  public static void main(String args[])
  {
    rec r = new rec();
    Scanner sc = new Scanner(System.in);
    int res, count=0;
    System.out.println("Enter a number: ");
    res = sc.nextInt();
   
    System.out.println("Prime numbers next to "+res+ " are: ");
    for(int j=0; j<5; j++)
    {
        res = r.func(res);
        System.out.println(res);  
        res++;
    }

  }
}


class rec
{
    int func(int n)
    {
        int count = 0;
        for(int i=2; i<=n; i++)
        {
            if(n % i == 0)
            {
                count++;
            }
        }

        if(count < 2)
        {
            //System.out.println(n);
            return n;
            //break;
        }
        else
        {
            n++;
            return func(n);
   
        }
      
    }
}
 

Output:

 

Comments

Popular Posts