Write a program to input any integer number and check whether the given number is pal-prime number or not. (A number is pal-prime if it is prime and palindrome both)
(Some of pal-prime numbers are 131, 151, 181, 191,919 etc.)
Answers
Answered by
1
Solution.
The problem is solved using language - Java.
import java.util.*;
public class PalPrime{
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
int a;
System.out.print("Enter a number: ");
a=sc.nextInt();
if(isPalPrime(a))
System.out.println("Pal Prime.");
else
System.out.println("Not Pal Prime.");
}
static boolean isPalPrime(int n){
int n1=n,s=0;
for(;n1!=0;n1/=10)
s = s * 10 + n1 % 10;
int c=0;
for(int i=1;i<=n;i++){
if(n%i==0)
c++;
}
return c==2 && s==n;
}
}
Explanation.
- The isPalPrime() function checks whether a number is palindrome as well as prime or not.
- The function is then called in main() to check whether the entered number is pal prime or not.
See attachment for output.
Attachments:


Similar questions