import java.io.*;
import java.lang.*;
class Prime
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the value: ");
        int n=Integer.parseInt(br.readLine());
        System.out.println("Prime numbers are: ");
        for(int i=1; i<=n; i++)
        {
            int nf=0;
            for(int j=1; j<=(i/2); j++)  //( i/2) reduces no.of comparisions
            {
                if(i%j==0)
                    nf++;
            }
            if(nf==1)
                System.out.print(" "+i);

        }
    }
}

 

OUTPUT:

Enter the value:
30
Prime numbers are:
1  2  3  5  7  9  11  13  17  19  23 29