-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmirp_Number.java
64 lines (61 loc) · 2.02 KB
/
Emirp_Number.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// In this section, we will learn what is an emirp number and also create Java
// programs to check if the given number is emirp or not. The emirp number Java
// program frequently asked in Java coding tests to check the logic of the
// programmer.
// Emirp Number
// A number is called an emirp number if we get another prime number on
// reversing the number itself. In other words, an emirp number is a number that
// is prime forwards or backward. It is also known as twisted prime numbers.
// Note: Palindrome primes are excluded.
import java.io.*;
import java.util.*;
class Emirp_Number
{
//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.");
}
}