In this tutorial you will learn about the Program to check Emirp Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Emirp Numbers in Java using Java programming.
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.
- For loop in Java.
What is Emirp Number?
A prime number 79 means that divisible by 1 and itself only. On overturn the number, we get a number 97 which is also a prime number. so we can say that number 79 and 97 both are prime numbers.
Example:
“17 = 71” both are Prime number so they are Emirp number
“19 = 91 ” Both are not Prime number so they are notEmirp number.
“79 = 97” both are prime number so they are Emirp number.
Program to check Emirp Number in Java
In this our program we will find given number is a Emirp number or not in using java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the a number.Later we find number is Emirp number or not let’s have a look at the code.
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 38 39 40 41 42 43 44 45 46 47 48 49 |
// Java programt o find given number is Emrip number.. import java.util.Scanner; public class Emirp{ public static void main(String args[]) { // Declaring variable and assign values.. Scanner in = new Scanner(System.in); System.out.print("Enter any Number: "); int number = in.nextInt(); // finding the number is prime or not... int count = 0; for (int i = 1; i <= number; i++) { if (number % i == 0) { count++; } } // if number is prime then reverse the number and check again same conditon.. if (count == 2) { int temp = number; int revrse = 0; while(temp != 0) { int digit = temp % 10; temp /= 10; revrse = revrse * 10 + digit; } int count1 = 0; for (int i = 1; i <= revrse; i++) { if (revrse % i == 0) { count1++; } } // printing number is Emirp or not if (count1 == 2) System.out.println(number +" is a Emirp Number"); else System.out.println(number +" is Not Emirp Number"); } else System.out.println("Not Emirp Number"); } } |
Output
Emirp Number.
Not a Emirp Number.
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold entered number.
After declaring variables in the next statement user will be prompted to enter a value and which will be assigned to variable ‘number’.
Logic to find Emirp Number
- take a number form user and initialize the number (number) to a variable
- First of all find given number is number prime or not.
- If not, break the execution and exit.
- If number is a prime,then find the reverse (reverse ) of the given number (number).as shown in image below.
- Now reverse the number (reverse) is prime or not as shown above.
- If not, print number (n) is not emirp.