Twisted Prime 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.
Twisted Prime number is also called Emirp Number.

Example : Input = 79
                  Reverse = 97
Output : Twisted Prime

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.

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


All the best :)

Comments

Post a Comment

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA