In this section, we will explore the concept of emirp numbers and demonstrate how to create Java programs to determine whether a given number qualifies as an emirp number. The Java program to identify emirp numbers is commonly included in Java coding assessments as a means to assess a programmer’s logical reasoning skills.
What is an Emirp Number?
An emirp number is a special type of number that exhibits a unique property. Specifically, when an emirp number is reversed, it results in another prime number. In simpler terms, an emirp number is a prime number that remains prime when its digits are reversed. Emirp numbers are also referred to as twisted prime numbers.
Note: Palindrome primes are excluded.
Emirp Number Example
Let’s consider the number 79 and determine whether it qualifies as an emirp number.
We observe that 79 is a prime number, meaning it is only divisible by 1 and itself. When we reverse the digits of 79, we obtain 97, which is another prime number. Thus, both 79 and 97 are prime numbers. Consequently, 79 can be classified as an emirp number. The same process can be applied to other numbers as well.
Some examples of emirp numbers include 13, 199, 107, 113, 1399, 1583, 1201, and 3049, among others.
Steps to find Emirp Number
- Read or initialize a number (n).
- First, check the given number (n) is prime or not.
- If not, break the execution and exit.
- If prime, find the reverse (r) of the given number (n).
- Check the reverse number (r) is prime or not.
- If not, print number (n) is not emirp.
- If prime, print the given number (n) as an emirp number.
Emirp Number Java Program
EmirpNumberExample1.java
import java.io.*; import java.util.*; public class EmirpNumberExample { //function that checks the given number is prime or not public static boolean isPrime(int n) { //base case if (n <= 1) return false; //loop executes from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) //returns false if the condition returns true return false; //returns true if the condition returns false return true; } //function that checks if the given number is emirp or not public static boolean isEmirp(int n) { //checks if the given number is prime or not if (isPrime(n) == false) return false; //variable that stores the reverse of the number int reverse = 0; //the while loop executes until the specified condition becomes false while (n != 0) { //finds the last digit of the number (n) int digit = n % 10; //finds the reverse of the given number reverse = reverse * 10 + digit; //removes the last digit n = n/10; } //calling the user-defined function that checks the reverse number is prime or not return isPrime(reverse); } //driver code public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter a number to check: "); //reading an integer from the user int n=sc.nextInt(); if (isEmirp(n) == true) System.out.println("Yes, the given number is an emirp number."); else System.out.println("No, the given number is not an emirp number."); } }
Output 1:
Enter a number to check: 1399 Yes, the given number is an emirp number.
Output 2:
Enter a number to check: 14 No, the given number is an emirp number.
Stay updated with tutorials.freshersnow.com to enhance your knowledge, just like learning about Emirp Numbers in Java.