Created
April 18, 2015 20:23
-
-
Save bonzaiferroni/2e87a7666a7fa37ed399 to your computer and use it in GitHub Desktop.
PrimeNumber
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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