JAVA Program for TWISTED PRIME number.

Monday, December 16, 2019

A twisted prime number is a number which is prime as well as it's reverse is also Prime.

Program for check no is twisted prime no or not.

if twisted prime no. then print("Yes")
if not then ("No")

Solution :


import java.util.Scanner;

public class Prime {

static boolean isPrime(int n)
{
boolean flag=true;
if(n>0 &&n
{
flag= true;
}
else 
{
for(int i=2;i
{
if(n%i==0)
{
flag=false;
break;
}
}
}
return flag;
}
static int rev(int n)
{
int res=0;
while(n>=1)
{
int mod=n%10;
n=n/10;
res=res*10+mod;
}
return res;
}

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i
{
a[i]=sc.nextInt();
}
for(int i=0;i
{
if(isPrime(a[i]))
{
int x=rev(a[i]);
if(isPrime(x))
{
System.out.println("Yes");
}
else
System.out.println("No");
}
else
System.out.println("No");
}

}

}

Comments