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

Proaram4 Design and implement a Java program for programming exercise 6.26, page

ID: 3594280 • Letter: P

Question

Proaram4 Design and implement a Java program for programming exercise 6.26, page 240 (name it PalindromicPrime), to display the first 100 numbers, 10 per line separated by exactly one space, that are both palindrome and prime. Write two methods (each taking a single parameter) IS Palindome ( ) to check if a number is palindrome or not (check Lab 7, Exercise #3) isPrime ) to check if a number is prime or not (see listing 6.7 page 216) All output is to be produced by the main method. Document your code and organize the outputs using appropriate formatting techniques

Explanation / Answer

//This program is displying 100 numbers having 10 numbers per line.

public class PalindromicPrime

{

public static void main(String[] args)

{ final int total = 100;

final int displayPerLine = 10;

int count = 1;

int number = 2;

while (count <= total)

{

if (isPrime(number) && isPalindrome(number))// to check whether no is prime &palindrome or not

{

System.out.print(number + " ");

if (count % displayPerLine == 0)

System.out.println();

count++;

}  

number++;

}

}

/* Method to check number is Prime */

public static boolean isPrime(int num)

{

if (num == 2)

return true;

for (int divisor = 2; divisor <= num/2; divisor++)

{

if (num % divisor == 0)

return false;

}

return true;

}

public static int reverse(int num)

{

int test = 0;

while (num != 0)

{

int lastdigit = num % 10;

test = test * 10 + lastdigit;

num = num / 10;

}

return test;

}

/* Method to check number is Palindrome */

public static boolean isPalindrome(int num)

{

return num == reverse(num);

}

}

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