Skip to content

Instantly share code, notes, and snippets.

@bonzaiferroni
Created April 18, 2015 20:23
Show Gist options
  • Save bonzaiferroni/2e87a7666a7fa37ed399 to your computer and use it in GitHub Desktop.
Save bonzaiferroni/2e87a7666a7fa37ed399 to your computer and use it in GitHub Desktop.
PrimeNumber
import java.util.Scanner;
/**
* Created by this busta (Luke) on 4/18/2015.
*/
public class PrimeNumberRecursive {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
while (true) {
System.out.print("Enter a prime number (enter 0 to exit): ");
int test = console.nextInt();
if (test == 0) {
break;
}
boolean prime = isPrime(test, 2);
System.out.print(test + " is ");
if (!prime) System.out.print("not ");
System.out.println("a prime number");
}
}
public static boolean isPrime(int n, int d) {
if (d < n) {
if (n % d == 0) return false;
return isPrime(n, d+1);
} else {
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment