Problem: Write a Java program to check whether the given number is Twisted Prime or not.

A number is said to be Twisted Prime if it is a prime number and its reverse is also a prime number.


Twisted Prime

Steps to check Twisted Prime in Java:

  1. Input a number.
  2. Check if the number is prime.
  3. If the number is prime, compute its reverse.
  4. Check if the reverse is prime.
  5. If both step-2 and step-4 are true, then the input number is Twisted Prime, else not.

Here is the implementation of the steps in Java:


Enter a number: 31
Twisted Prime

In the above program, the isPrime(int n) function returns true if the passed number (i.e. n) is a prime number, otherwise it returns false.

We use the isPrime(int n) to check whether the input and its reverse are prime or not.

If anyone of them is not a prime number then it means input is not a Twisted Prime.

If both the numbers are prime then the input is said to be a Twisted Prime number.

In this programming example, we learned to check Twisted Prime number in Java programming language.

Leave a Reply

1 × 4 =