Java Program to Find Next Prime

This is the Java program to find the next prime
NextPrime[n]
gives the next prime above n

[cc lang=”java”]
import java.io.*;

class nxtprm
{
public static void main(String[] args)
{
int n=0,j,flag=0,i=0;
System.out.println(“Enter the Limit”);
DataInputStream in = new DataInputStream(System.in);
try
{
n=Integer.parseInt(in.readLine());
}

catch(Exception e){}

while(i<=n) { flag=0; for(j=1;j<=i;j++) { if(i%j==0) { flag++; } } if(flag==2){System.out.println(" "+i);} i++; } } } [/cc]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *