# How To Check Whether a Number is an Empire Number or Not in Java?

A number is said to be an empire number, if after reversing a prime number we get another prime number.

In this article, we will see how to check if a number is an empire number by using Java programming language.

### To show you some instances

Instance-1

Input number is 13.

Let’s check it by using the logic of the Empire number.

13 is a prime number.

If we reverse 13, then we get = 31

As we notice here both the reversed number and input number both are prime numbers.

Hence, 13 is an empire number.

Instance-2

Input number is 79.

Let’s check it by using the logic of the Empire number.

143 is a prime number.

If we reverse 79, then we get = 97

As we notice here both the reversed number and input number both are prime numbers.

Hence, 79 is an empire number.

Instance-3

Input number is 53.

Let’s check it by using the logic of the Empire number.

53 is a prime number.

If we reverse 53, then we get = 35

As we notice here the reversed number is not a prime number.

Hence, 53 is not an empire number.

### Algorithm

Step-1 - Get the input number either by initialization or by user input.

Step-2 - Check if the input number is prime number or not.

Step-3 - Then take a user defined method to check if the input number is an empire number or not.

Step-4 - Inside the method, get the desired reverse number and check whether that number is a prime number or not.

Step-5 - If both the numbers are prime numbers then the original number is called an empire number else not.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using static Input Value with user defined method

• By Using user Input Value with user defined method

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Input Value with User Defined Method

In this approach, we declare a variable with a static input and then by using the algorithm we can check whether the number is an Empire number or not.

### Example

import java.util.*;
public class Main {
public static void main (String args[]) {

//declare a variable and pass the static input
int inp= 13;

//checking for empire number
if (checkEmpire(inp) == true)
System.out.println(inp + " is an empire number.");
else
System.out.println(inp + " is not an empire number.");
}

//user defined function to find the prime number
public static boolean checkPrime (int n) {
if (n <= 1)
return false;

//initiate the loop to check the prime number
for (int i = 2; i < n; i++)
if (n % i == 0)

//if condition true then return false
return false;

//otherwise return true
return true;

}
//function that checks if the given number is empire or not
public static boolean checkEmpire(int inputNumber) {

//check whether the input number is prime number or not
if (checkPrime (inputNumber) == false)
return false;

//declare a variable to store the reverse of input number
int reverse = 0;

//initiate a loop to calculate the reverse number
while (inputNumber != 0) {

//collect the last digit
int digit = inputNumber % 10;

//store the reversed value
reverse = reverse * 10 + digit;

//remove the last digit from input number
inputNumber = inputNumber / 10;
}
//calling the user-defined function that checks the reverse number is prime or not
return checkPrime(reverse);
}
}


### Output

13 is not an empire number.


## Approach-2: By Using User Input Value with User Defined Method

In this approach, we ask the user to enter a number as input number and pass this number as a parameter in a user defined method, then inside the method by using the algorithm we can check whether the number is an Empire number or not.

### Example

import java.util.*;
public class Main {
public static void main (String args[]) {

//create object of Scanner class
Scanner sc=new Scanner(System.in);

//ask user to give the input
System.out.print("Enter a number: ");

//declare a variable and store the input value
int inp=sc.nextInt();

//checking for empire number
if (checkEmpire(inp) == true)
System.out.println(inp + " is an empire number.");
else
System.out.println(inp + " is not an empire number.");
}

//user defined function to find the prime number
public static boolean checkPrime(int n) {
if (n <= 1)
return false;

//initiate the loop to check the prime number
for (int i = 2; i < n; i++)
if (n % i == 0)

//if condition true then return false
return false;

//otherwise return true
return true;

}

//function that checks if the given number is empire or not
public static boolean checkEmpire(int inputNumber) {

//check whether the input number is prime number or not
if (checkPrime (inputNumber) == false)
return false;

//declare a variable to store the reverse of input number
int reverse = 0;

//initiate a loop to calculate the reverse number
while (inputNumber != 0) {

//collect the last digit
int digit = inputNumber % 10;

//store the reversed value
reverse = reverse * 10 + digit;

//remove the last digit from input number
inputNumber = inputNumber / 10;
}

//calling the user-defined function that checks the reverse number is prime or not
return checkPrime(reverse);
}
}


### Output

Enter a number: 79
79 is an empire number.


In this article, we explored how to check a number whether it is an Empire number or not in Java by using different approaches.