Write a program to print first n prime numbers in Java1 min read
Here is my solution to
Write a program to print first n prime numbers in Java
Program:
class Primeno { public static void main(String arg[]) { int n,c=0,j=2; n=Integer.parseInt(arg[0]); System.out.println(" The first "+n+" prime nos. are "); for(int i=0;i<n;) { c=0; for(int k=2;k<=j/2;k++) { if(j%k==0) c++; } if(c==0) { System.out.print("\t"+j); i++; } j++; } } }
Output:
Vishal Sharma Answered question