public class PrimeChecker
1: //PrimeChecker.java
3: public class PrimeChecker
4: {
5: // Returns 0 if value is not prime, 1 if value is prime
6: public static int isPrime
7: (
8: int testVal,
9: int divVal
10: )
11: {
12: // Base cases 0 and 1: 0 and 1 are not prime, testVal is not prime
13: if (testVal == 0 || testVal == 1)
14: return 0;
16: // Base case 2: testVal only divisible by 1, testVal is prime
17: if (testVal == 2)
18: return 1;
20: // Recursive Case
21: // Check if testVal can be evenly divided by divVal
22: // Hint: use the % operator
23: // If not, recursive call to isPrime with testVal and (divVal - 1)
24: if (divVal == 1)
25: return 1;
26: else if (testVal % divVal == 0)
27: return 0;
28: else
29: return isPrime(testVal, divVal - 1);
30: }
32: public static void main(String[] args)
33: {
34: int primeCheckVal; // Value checked for prime
36: // Check primes for values 1 to 10
37: for (primeCheckVal = 1; primeCheckVal <= 10; ++primeCheckVal)
38: {
39: if (isPrime(primeCheckVal, (primeCheckVal - 1)) == 1)
40: {
41: System.out.println(primeCheckVal + " is prime.");
42: }
43: else
44: {
45: System.out.println(primeCheckVal + " is not prime.");
46: }
47: }
48: }
49: }