Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the program. The following program called PalindromePrime should print

ID: 3622340 • Letter: C

Question

Complete the program.

The following program called PalindromePrime should print out
the first 100 palindromic primes in reverse order (i.e., from
the largest to the smallest). A number is a prime if it is
divisible only by itself and by 1. For example, 13 is a prime. A
number is a palindrome if it is itself in reverse. For example,
121 is a palindrome. Thus, 11 is a palindromic prime, since it
meets both conditions (so are 2, 3, 5, and 7).



The output of the program is:

> 94049

> 93739

> ...

> ...

> 3

> 2

(Note: 94049 is the 100th palindrome prime, 93739 is the 99th
palindrome prime, 3 is the 2nd palindrome prime, and 2 is the
1st palindrome prime).



The strategy we take is as follows:

First, since we want to print the primes in reverse order, we
store them in an array as we find them, and then simply print
the array out in reverse order. To find a palindrome prime, we
split up the work into checking if a number is a palindrome and
if it is a prime. To check if it is a palindrome, we simply
compute the reverse of the number and check if it is the same as
the number.



NOTE: While the program below looks long, it is in actuality
quite simple, since I have filled most of the complex parts. The
comments in the program should really help you - please read
them. Also, if you see a "backslash" before any "left square
bracket", ignore it, since it was placed there just to allow
Blackboard to correctly parse the question.



public class PalindromePrime {

public static void main(String[] args) {

int count = 0;

int number = 2;

pprime =
new int[100];



while (count < 100 ){

// Check whether the number is a prime and a palindrome

if (isPrime(number) &amp;&amp; ) {

// store the prime into the array

= number;

count++; // Increase count

}

number++ ; // Go to the next number

}



// iterate through the array in reverse order

for (int i=; ; i--)

System.out.println();

}





// This method checks if a number is a prime

public static
isPrime(int num) {

// Note that any number is a prime only if it is only
divisible by 1 or by itself

for (int i = 2; i

Explanation / Answer

public class PalindromePrime {

   
    public static void main(String[] args)
    {
       int count = 0;
       int number = 2;
       int[] pprime = new int[100];
       while (count < 100 ){
       // Check whether the number is a prime and a palindrome
    if (isPrime(number) && isPalindrome(number) ) {
    // store the prime into the array
    pprime[count]= number;
    count++; // Increase count
    }
    number++ ; // Go to the next number
    }
// iterate through the array in reverse order
for (int i=99;i>=0 ; i--)
System.out.println(pprime[i] +" ");
}
// This method checks if a number is a prime
public static boolean isPrime(int n) {
    for(int i=2;2*i<n;i++) {
        if(n%i==0)
            return false;
    }
    return true;
}

public static boolean isPalindrome(int num)
{
          int n = num; //used at last time check

          int reverse=0,remainder;

          while(num > 0){

                remainder = num % 10;

                reverse = reverse * 10 + remainder;

                num = num / 10;

           }

          if(reverse == n)

              return true;

          return false;

     }

}


  


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote