
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- How To Check Whether a Number is an Ugly Number or Not in Java?
- Check Whether a Number is an Autobiographical Number or Not in JAVA
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- How To Check Whether a Number is Strontio Number or Not in Java?
- How To Check Whether a Number is Tcefrep Number or Not in Java?
- How To Check Whether a Number is Tech Number or Not in Java?
- How To Check Whether a Number Is a Pronic Number or Not in Java?
- How To Check Whether a Number Is a Bouncy Number or Not in Java?
- How To Check Whether a Number is a Evil Number or Not in Java?
- How To Check Whether a Number Is a Fascinating Number or Not in Java?
- How To Check Whether a Number is a Harshad Number or Not in Java?
- How To Check Whether a Number Is a Insolite Number or Not in Java?
- How To Check Whether a Number is a Keith Number or Not in Java?
- How To Check Whether a Number Is a Niven Number or Not in Java?
- How To Check Whether a Number is a Sunny Number or Not in Java?
