Computer Science, asked by jaiswalishita009, 1 month ago

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 anindyaadhikari13
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() fu‎nction checks whether a number is palindrome as well as prime or not.
  • The fu‎nction is then called in main() to check whether the entered number is pal prime or not.

See attachment for output.

Attachments:
Similar questions