KnowledgeBoat Logo

Computer Applications

Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.]
For example: 11, 101, 131, 151,

Java

Java Nested for Loops

74 Likes

Answer

public class KboatPrimePalindrome
{
    public void displayPrimePalindrome() {

        int count = 0;

        for (int i = 10; i <= 1000; i++) {

            int num = i, revNum = 0;
            while (num != 0) {
                int digit = num % 10;
                num /= 10;
                revNum = revNum * 10 + digit;
            }


            if (revNum == i) {
                boolean isPrime = true;
                for (int j = 2; j <= i / 2; j++) {

                    if (i % j == 0) {
                        isPrime = false;
                        break;
                    }

                }
                if (isPrime) {
                    System.out.print(i + " ");
                    count++;
                    if (count == 10) {
                        System.out.println();
                        count = 0;
                    }
                }
            }
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to display all prime palindrome numbers between 10 and 1000. [Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.] For example: 11, 101, 131, 151,

Answered By

30 Likes


Related Questions