Using \"Eclipse IDE for Java EE Developers\" complete the problem below: (Palind
ID: 669426 • Letter: U
Question
Using "Eclipse IDE for Java EE Developers" complete the problem below:
(Palindromic Prime) A palindromic prime is a prime number and also palindromic. For example, 131 is a prime and also a palindromic prime, as are 313 and 757. Write a program that displays the first 100 palindromic prime numbers. Display 10 numbers per line, separated by exactly one space, as follows:
2 3 5 7 11 101 131 151 181 91
313 353 373 383 727 757 787 797 919 929
(Hints) you need to check if an integer is a palindromic prime by using the following two methods. The first is to see if an integer is a prime number and the second is to get the reversal of an integer.
public static boolean isPrime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
static int reversal(int number) {
int result = 0;
while (number != 0) {
int lastDigit = number % 10;
result = result * 10 + lastDigit;
number = number / 10;
}
return result;
}
Explanation / Answer
package com.org.nancy.solutions;
/*
* Author : Nancy
*/
public class PalandromeAndPrime {
// Checking that number is prime or not?
public static boolean isPrime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
// Checking that number is palandrome or not?
public static int reversal(int number) {
int result = 0;
while (number != 0) {
int lastDigit = number % 10;
result = result * 10 + lastDigit;
number = number / 10;
}
return result;
}
/*
* variable : counter is the number of palandrome prime need to be displayed.
*/
public static void printPalandromePrime(int counter) {
for (int i = 2; counter > 0; i++) {
boolean primeRes = isPrime(i);
int palandromeRes = reversal(i);
if (primeRes == true && palandromeRes == i) {
//breaking the line after 10 entries as per solution requirement.
if (counter % 10 == 0)
System.out.println("");
//Printing of numbers
System.out.print(i + " ");
counter--;
}
}
}
public static void main(String[] args) {
//You can pass any number in this function and get as many as palandrome prime numbers.
printPalandromePrime(100);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.