In java, WAP to accept a number and check it's a twisted prime or not using 3 functions:-
Input, isprime(), display ()
Answers
Answer:
Twisted Prime Number
A number is said to be twisted prime if it is a prime number and reverse of the number is also a prime number.
Examples:
Input : 97
Output : Twisted Prime Number
Explanation: 97 is a prime number
and its reverse 79 is also a prime
number.
Input : 43
Output : Not a Twisted Prime Number
Explanation: 43 is a prime number
but its reverse 34 is not a prime
number.
hi mate,
Answer:
CODE :
import java.util.*;
class twisted_prime
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to check whether it is twisted prime or not");
int n=sc.nextInt();
int f=0;
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
{
f=1;
}
if(f==1)
{
int rev=0;
int cpy=n;
c=0;
int d=0;
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
for(int i=1;i<=rev;i++)
{
if(rev%i==0)
{
c=c+1;
}
}
if(c==2)
{
System.out.println("Twisted prime number");
}
else
{
System.out.println("Not a twisted prime number");
}
}
else
{
System.out.println("Not a twisted prime number");
}
}
}
Explanation:
The number is taken as input with the help of scanner class .
Then we check whether the number is prime or not by using for loop and counter variable .
Then we can reverse the number using digit extraction .
After that check whether the reverse of the number is prime or not .
Then if both conditions are true we will print that the numbers are twisted primes otherwise not .
i hope it helps you..