Given a number the task is to check whether the number is Prime or not. If not prime then print the next Prime Number.
Java
import java.util.*;
public class NextPrime
{
public static void main(String[] args)
{
int n=0,c=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
n=sc.nextInt();
for(int i=n;;i++)
{
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2&&i==n)
{
System.out.println("It is already a Prime Number");
break;
}
if(c==2)
{
System.out.println("Next Prime: "+i);
break;
}
c=0;
}
}
}Java