ISC Practicals and Theory - Emirp Number
Updated 21 Oct 2026
An emirp number, also called a twisted prime, is a prime number that is still a prime when its digits are reversed. This note gives the program written in the main method alone: it reverses the digits, then counts the factors of the number and of its reverse.
Definition
A prime number $N$ has exactly two factors, $1$ and $N$. A number is an emirp number (twisted prime) if $N$ is prime and the number formed by reversing its digits is also prime.
- $13$ is an emirp: $13$ is prime, its reverse $31$ is prime.
- $23$ is not an emirp: $23$ is prime, but its reverse $32$ is not.
- $12$ is not an emirp: $12$ is not prime.
- $11$ is an emirp in this program: $11$ is prime and its reverse $11$ is prime. The program follows the wording "prime, and its reverse is prime". Some books also require the reverse to be a different number, which would leave out $11$, $101$ and other palindromic primes.
Algorithm
- Accept the number
num. Ifnum < 1, displayInvalid inputand stop. - Reverse the digits: copy
numintonand setrev = 0. Whilen > 0, take the last digitd = n % 10, dorev = rev * 10 + dandn = n / 10. - Count the factors of
numinc1: forifrom 1 tonum, add 1 toc1whenevernum % i == 0. - Count the factors of
revinc2in the same way. - A prime number has exactly two factors. If
c1 == 2andc2 == 2, display thatnumis an emirp number. Otherwise display that it is not.
Program
Everything is written inside main: one loop reverses the number and two for loops count the factors of the number and of its reverse.
import java.util.Scanner;
public class Emirp
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt(); // the number to be checked
if (num < 1)
{
System.out.println("Invalid input");
}
else
{
int n = num, rev = 0; // rev stores the reverse of num
while (n > 0)
{
int d = n % 10; // last digit
rev = rev * 10 + d;
n = n / 10;
}
int c1 = 0; // number of factors of num
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
c1++;
}
int c2 = 0; // number of factors of rev
for (int i = 1; i <= rev; i++)
{
if (rev % i == 0)
c2++;
}
if (c1 == 2 && c2 == 2) // a prime has exactly two factors
System.out.println(num + " is an emirp number");
else
System.out.println(num + " is not an emirp number");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
num | int | The number entered by the user |
n | int | Copy of num whose last digit is removed while reversing |
rev | int | The number with its digits reversed |
d | int | The last digit of n in the current pass |
c1 | int | Number of factors of num |
c2 | int | Number of factors of rev |
i | int | Loop counter that tries each possible factor (in both for loops) |
Sample input and output
Sample 1
Enter a number: 13
13 is an emirp numberSample 2
Enter a number: 23
23 is not an emirp numberSample 3
Enter a number: 11
11 is an emirp numberSample 4
Enter a number: -5
Invalid inputDry run
Input: num = 13
First the digits of 13 are reversed:
| Pass | d = n % 10 | rev = rev * 10 + d | n = n / 10 |
|---|---|---|---|
| Start | - | 0 | 13 |
| 1 | 3 | 3 | 1 |
| 2 | 1 | 31 | 0 |
The loop stops because n = 0, so rev = 31.
Then the factors of 13 and of 31 are counted. In each for loop, the remainder is 0 only for $i = 1$ and $i$ equal to the number:
| Number | Factors found | count | Prime? |
|---|---|---|---|
13 (c1) | 1, 13 | 2 | true |
31 (c2) | 1, 31 | 2 | true |
Both conditions are true (c1 == 2 and c2 == 2), so the program shows that $13$ is an emirp number, as in the first sample run.
Variations
- Range: "Display all emirp numbers between m and n."
- Recursive technique: "Check whether a number is an emirp number, using a recursive method to reverse the digits."
- Twin primes: "Display all twin prime pairs in a given range."
- Palindrome prime: "Check whether a number is a prime whose reverse is the same number."