Computer Applications
A palindromic prime is a prime number and also palindromic. For example, 131, 313, and 757 are prime numbers and also palindromic prime numbers. Write a program that displays the first 100 palindromic prime numbers.
Java
User Defined Methods
47 Likes
Answer
public class KboatPalindromicPrime
{
public static boolean isPrime(int n) {
int c = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
boolean prime = c == 2;
return prime;
}
public static boolean isPalindrome(int n) {
int num = n, revNum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
revNum = revNum * 10 + digit;
}
boolean palin = revNum == n;
return palin;
}
public static void main(String args[]) {
int x = 1, count = 0;
while (count < 100) {
if (isPrime(x) && isPalindrome(x)) {
count++;
System.out.println(x);
}
x++;
}
}
}Output

Answered By
22 Likes