To find whether a number is twisted prime or not between 1 to 100 in Java

In this Java tutorial, we will learn about twisted prime numbers in Java. The basic definition of a twisted prime number. We will also learn how to check whether a number is a twisted prime number or not. We will also print all the twisted prime numbers from 1 to 100.

Definition of a twisted prime number

If the number and its reverse is a prime number then it is a twisted prime number.

example:Input:97 is a twisted prime number.As the reverse of the number is 79 which is a prime number.
example: Input  43 is a prime number. The reverse of the number is not prime. Thus 34 is not prime. So, the number is not twisted prime.

Find all prime numbers less than or equal to N in Java

Java Program To Check A Number Is Prime or Not

The code for twisted prime numbers in Java

public static void main(String args[])
{
for(int i=1;i<100;i++)
{
boolean b=twistedPrime(i);
if(b==true)
{
int r=reverse(i);
boolean bb=twistedPrime(r);
if(bb==true)
System.out.println(i);
}
}
}
public static boolean twistedPrime(int n)
{
int f=0;
for(int i=2;i<n;i++)
{
if(a%i==0)
{
f=1;
break;
}
}
if(f==0)
return true;
}
public static int reverse(int n)
{
int rev=0;
int k=0;
while(n!=0)
{
int d=n%10;
rev=(rev*10+d);
n=n/10;
}
return rev;
}
}

 



OUTPUT:

2
3
5
7
11
13
17
31
37
71
73
79
97

Thus in the above-mentioned code, we are trying to check whether the number is twisted prime or not. At first in the main block, we take a boolean variable b.Which calls the function twistedPrime() function. Which returns either a true or false.

You may also learn,

In the twistedPrime() function first, we will check number is prime or not. If the number is a prime number then we call reverse().In this function, the original number is reversing itself.

Again we call the twistedPrime() function and will check whether it is prime or not. If the number is prime then the number is twisted prime. Similarly, we continue this process for 100 times and print the twisted prime numbers from 1 to 100.

Leave a Reply

Your email address will not be published. Required fields are marked *