
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How To Check Whether a Number is an Empire Number or Not in Java?
What is Empire Number?
A number is said to be an Empire number if it is a prime number, and when we reverse that number, we should get another prime number. For example, let's consider the number 17. We know that 17 is a prime number, and if we reverse this number, we get 71, which is also a prime number. Therefore, 17 is known as an empire number.
Here are some other examples of prime numbers such as 11, 13, 17, etc.
Input & Output Scenarios
Below are a few input and output scenarios that help to understand the problem implementation:
Scenario 1
Suppose the given number is 13:
Input: n = 13 Output: Yes Calculation: Reverse of 13 is 31.
Since both the number 13 and its reverse, 31, are prime numbers, 13 is considered an empire number.
Scenario 2
Suppose the input number is 53:
Input: n = 53 Output: No Calculation: Reverse of 53 is 35.
Since both the number 53 and its reverse, 35, are not prime numbers, 53 is not an empire number.
Example 1
The following program checks whether the number 31 is an Empire number. After reversing the number, if the resultant number (i.e., 13) is also a prime number, then the number is called an empire number:
public class empireNumber { //method to check for prime number public static boolean checkPrime (int n) { if (n <= 1){ return false; } for (int i = 2; i < n; i++) if (n % i == 0){ return false; } return true; } public static void main (String args[]) { int num = 13; System.out.println("The given number is: " + num); if (checkPrime(num) == false){ System.out.println("No! " + num + " is not a empire number"); } int reverse = 0; while (num != 0) { int digit = num % 10; reverse = reverse * 10 + digit; num = num / 10; } boolean result = checkPrime(reverse); if(result){ System.out.println("Yes! " + reverse + " is a number a empire number"); } else{ System.out.println("No! " + reverse + " is a number a empire number"); } } }
Following is the output of the above program:
The given number is: 13 Yes! 31 is a number a empire number
Example 2
In the example below, we define a method named checkEmpire(), which reverses the given number 89 and checks whether the reversed number is prime or not if it is a prime number is an empire or not:
public class empireNumber { //method to check for prime number public static boolean checkPrime (int n) { if (n <= 1){ return false; } for (int i = 2; i < n; i++) if (n % i == 0){ return false; } return true; } //method to check empire number public static boolean checkEmpire(int num){ if (checkPrime(num) == false){ System.out.println("No! " + num + " is not a empire number"); } int reverse = 0; while (num != 0) { int digit = num % 10; reverse = reverse * 10 + digit; num = num / 10; } boolean result = checkPrime(reverse); if(result){ return true; } return false; } public static void main (String args[]) { int num = 89; System.out.println("The given number is: " + num); //calling the checkEmpire() method to check empire number System.out.println("Is the number " + num + " is empire number? " + checkEmpire(num)); } }
The above program produces the following output:
The given number is: 89 Is the number 89 is empire number? false