Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created May 1, 2020 20:01
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 alvareztech/f2dd70b6bb73ff7df64ef89ce5b06585 to your computer and use it in GitHub Desktop.
Save alvareztech/f2dd70b6bb73ff7df64ef89ce5b06585 to your computer and use it in GitHub Desktop.
Prime Numbers with just a while in Java.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner e = new Scanner(System.in);
int n = e.nextInt();
int c = 1;
int p = 2;
int divisor = 2;
while (c <= n) {
if (p % divisor == 0) {
if (p == divisor) {
System.out.print(p + ", ");
c++;
}
divisor = 2;
p++;
} else {
divisor++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment