java program to print 1 to n prime numbers

import java.util.*;
public class PrimeNumber
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n;
        boolean flag=true;
        n=sc.nextInt();
        for(int i=2;i<=n;i++)
        {
            for(int j=2;j<i;j++)
            {
                if(i%j==0)
                   flag=false;
            }
            if(flag)
               System.out.print(i+" ");
            flag=true;

        }
       
    }
}

Comments

Popular posts from this blog

Small virus code to hang your friend's system

How to use Remote Desktop