# Java Program to Check Twisted Prime Number

## Program to Check Twisted Prime Number

In this article we are going to understand what Twisted Prime number is and how we can check whether a number is Twisted Prime or not in Java with examples.

Twisted Primes are prime numbers whose reverse are also prime numbers.

 Example :

13: Twisted Prime number
19: Not Twisted Prime number
11: Twisted Prime number

In the above examples the numbers 13 and 11 are only Twisted primes. 91 is the reverse of second number 19 which s not a prime. Hence 19 is not a Twisted prime number.

Let’s see different ways to check twisted prime number.

Approach :

1. Enter/declare a number and store it .
2. We pass the number into a function that returns the reversed number, then both the number and the reversed number is checked for prime.
3. If both of the numbers are prime then the number is said to be twisted prime.

### Method-1: By Using Static Value

import java.util.Scanner;
public static void main(String args[])
{
//A number declared
int num = 17;

if((isPrime(num)) && (isPrime(reverseNum(num))))
{
System.out.println(num+" is Twist Prime");
}
else
{
System.out.println(num+" is Not Twist Prime");
}

}

//Function that returns reverse of a number
static int reverseNum(int num)
{
int rev=0, remainder = 0;
while (num != 0)
{

remainder = num % 10;
rev = rev * 10 + remainder;
num = num / 10;
}

return rev;
}
// Function to check for prime
static boolean isPrime(int num)
{
int iter = 2;
boolean flag = true;
while (num > iter)
{
if (num % iter == 0)
{
flag = false;
break;
}
iter++;
}
return flag;
}
}

Output:

17 is Not Twist Prime

### Method-2: By User Input Value

import java.util.Scanner;
public static void main(String args[])
{
//Taking the number as input from the user using scanner class
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = scan.nextInt();

if((isPrime(num)) && (isPrime(reverseNum(num))))
{
System.out.println(num+" is Twist Prime");
}
else
{
System.out.println(num+" is Not Twist Prime");
}

}

//Function that returns reverse of a number
static int reverseNum(int num)
{
int rev=0, remainder = 0;
while (num != 0)
{

remainder = num % 10;
rev = rev * 10 + remainder;
num = num / 10;
}

return rev;
}
// Function to check for prime
static boolean isPrime(int num)
{
int iter = 2;
boolean flag = true;
while (num > iter)
{
if (num % iter == 0)
{
flag = false;
break;
}
iter++;
}
return flag;
}
}

Output:

Case-1

Enter a number : 13
13 is Not Twist Prime

Case-2

Enter a number : 23
23 is Not Twist Prime