Following code prints all prime numbers upto user defined N:
import java.util.*;
public class Prime {
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Max Limit");
int n=in.nextInt();
int a=2,p=0;
do
{
for(int c=2;c<=a/2;c++)
{
if(a%c==0)
{
p=1;
break;
}
}
if(p==0)
{
System.out.print(a+" ");
}
a++;
p=0;
}
while(a<=n);
}
}
Output:
import java.util.*;
public class Prime {
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Max Limit");
int n=in.nextInt();
int a=2,p=0;
do
{
for(int c=2;c<=a/2;c++)
{
if(a%c==0)
{
p=1;
break;
}
}
if(p==0)
{
System.out.print(a+" ");
}
a++;
p=0;
}
while(a<=n);
}
}
Output: