Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 6, 2018 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/6606e9fbac3032e3a5ff8d1961202aeb to your computer and use it in GitHub Desktop.
Save thmain/6606e9fbac3032e3a5ff8d1961202aeb to your computer and use it in GitHub Desktop.
public class FirstNPrimeNumbers {
static void printPrimeNos(int N){
int number = 2;
int primeCount = 0;
while(primeCount< N){
if(isPrime(number)){
System.out.print(number + " ");
primeCount++;
}
number++;
}
}
static boolean isPrime(int n){
for (int i = 2; i <=Math.sqrt(n) ; i++) {
if(n%i==0)
return false;
}
return true;
}
public static void main(String[] args) {
int N = 5;
printPrimeNos(N);
System.out.println();
N = 20;
printPrimeNos(N);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment