top of page
Writer's pictureSatyam Gupta

Twisted Prime Number in JAVA

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.


Example : Input = 79

                Reverse = 97

Output : It is Twisted Prime Number

 

Algorithm and Program Below :-


Algorithm :-

  • Input a Number

  • check the entered number is Prime or Not.

  • If it is Prime, Reverse the Number and check the reversed Number is prime or not.

  • If both the numbers are prime than Print Number is Twisted Prime.

  • If not than print Number is not Twisted Prime

 

Program :-


import java.io.*; import java.util.*; class Twisted_Prime {     public static void main(String args[])     {         Scanner in= new Scanner(System.in);         int n,p,i,rev,c=0,s=0,d=0;         System.out.println("Enter a Number");         n=in.nextInt(); // Input a Number         p=n; // copy of Input number         for(i=1;i<=n;i++) // Checking For prime or not         {             if(n%i==0)             {                 c++;             }         }         if(c==2)         {             System.out.println("Entered Number is Prime");             while(n>0) // Reversing the Prime Number             {                 rev=n%10;                 s=s*10+rev;                 n=n/10;             }             for(i=1;i<=s;i++) // Checking reverse number is Prime or not             {                 if(s%i==0)                 {                     d++;                 }             }             if(d==2)             {                 System.out.println("Entered Number is Twisted Prime : "+s);                 System.out.println("Original Number : "+p);             }             else             {                 System.out.println("Entered Number is not Twisted Prime : "+s);                 System.out.println("Original Number : "+p);             }         }         else         {             System.out.println("Entered Number is not Prime : "+p);         }     } // end of main } // end of class

 

Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.

All the best :)

Keep Learning :)

871 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page