Write a java program called PalindromicPrime that displays the first 100 numbers
ID: 3759006 • Letter: W
Question
Write a java program called PalindromicPrime that displays the first 100 numbers that are both a palindrome and a prime number. In your output you must show 10 numbers per line, and the numbers should each be separated by exactly one space.
Your program will have a main() method and two other methods called isPalindrome() and isPrime(). Your other two methods will have the following method headers:
Design the main() method of your program such that it has a loop going through all the numbers it needs to to find 100 numbers that meet the conditions. And it sends each number to your two methods to determine if that number meets the conditions.
Here is what the first two lines of your output should show:
Hint: The isPalindrome() method was done in Part 2 of Lab 8, and the isPrime() method is done in the Textbook in listing 6.7 on page 216.
Explanation / Answer
Solution for Above problem in java is below:
public class PalindromicPrime {
public static void main(String[] args) {
int count=1,number=2;
String result="";
while(count<=20){ // Change the number of digitis here example given first 20 numbers
if(Prime(number) && Palindromic(number))
{
if(count%10==0){
result += " "+number+" ";
}else{
result += " "+number;
}
count++;
}
number++;
}
System.out.print(result);
}
public static boolean Prime(int num){
int count = 0;
for(int divisor=2;divisor<=num/2;divisor++){
if(num%divisor==0){
return false;
}
}
return true;
}
public static boolean Palindromic(int num){
int result = 0;
int number = num;
while(num!=0){
int lastDigit= num%10;
result = result*10+lastDigit;
num /=10;
}
if(number==result){
return true;
}
return false;
}
}
Output is:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.