In this tutorial you will learn about the Program to check Twisted Prime Program in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Twisted Prime Program in Java.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- Function in java.
Twisted Prime Number
A given number called twisted prime number if it is a prime number and reverse of it also a prime number.
Some Examples of Twisted Prime number 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79.
Take a number : “97” is a prime number then
Twisted Prime Number “97” is a prime number
and its reverse “79” is also a prime number.
Program to check Twisted Prime Program in Java
In this program we would find the given number is twisted prime number or not , first will take the input from the user and check whether it is prime number or not. If it’s prime then we will twist the number and also check it is prime or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// java program to find twisyed prime number import java.util.*; class T_Prime { public static void main(String[] args) { // decalering variables.. int num, rev,sum=0 , count; Scanner sc = new Scanner(System.in); System.out.println("Enter a prime number?"); num = sc.nextInt(); // reversing a number while(num!=0) { rev = num%10; sum = sum*10 + rev; num= num/10; } // condition for twisted prime number count=0; for (int j = 2; j <= sum / 2; j++) { if ((sum % j) == 0) { count = 1; break; } } // condition for twisted prime number if (count == 0) System.out.println("Twisted Prime"); else System.out.println("Not Twisted Prime"); } } |
Output
Twisted Prime number
Not a prime number.
In the above program, we have first declared and initialized a set variables required in the program.
- num = it will hold entered number.
- rev = it will hold reverse or a number
- sum = for adding values
- count= counting the divisibility
- i= For iteration.
After Declaring variables we take a prime number from user.
First of all we reveres the given number and then check the reverse number is also a prime number or not.
In above image we reversing a number then at last we will check the reverse number is also prime or not .
if the reverse number is also a prime then we can say the entered prime number is twisted prime number.
else it is not a twisted prime number.