-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise06_26.java
More file actions
55 lines (49 loc) · 1.44 KB
/
Copy pathExercise06_26.java
File metadata and controls
55 lines (49 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jorda
*/
public class Exercise06_26 {
public static void main(String[] args){
int count = 0;
int numToTest = 2;
int line = 1;
while (count <= 100){
if((isPrime(numToTest)) && (isPalindrome(numToTest))){
count++;
if(line % 10 != 0){
System.out.print(numToTest + " ");
line++;
}else{
System.out.println(numToTest + " ");
line = 1;
}
}
numToTest++;
}
}
public static boolean isPrime(int number) {
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) { // If true, number is not prime
return false; // number is not a prime
}
}
return true; // number is prime
}
public static boolean isPalindrome(int number){
return (number == reverse(number));
}
public static int reverse(int number){
int reverse = 0;
while(number > 0){
int lastDigit = number % 10;
reverse = (reverse * 10) + lastDigit;
number /= 10;
}
return reverse;
}
}