Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications
A prime number is a number that has only two factors, 1 and itself.
An Emirp number is a number, that when reversed, gives a prime number.
Example: 13, when reversed gives 31, which is a prime number.
Note: Emirp numbers are not palindromes.
Palindromes are numbers, which when reversed , give the same number.
Example: 131 when reversed, given 131.
Hence, 131 is a palindrome.
131 is not an emirp.
Algorithm
- Start
- n:=0
- temp:=0
- rev=0
- digit:=0
- i:=0
- k:=0
- Print "Enter a number"
- Read n
- temp=n
- If temp>0, go to step 12 or else go to step 16
- digit=temp%10
- rev=rev*10+digit
- temp=temp/10
- Go to step 11
- If step 17==true && n!=rev, then go to step 26 or else go to step 27
- k=0. Go to step 18
- i=2. Go to step 19
- If i<n, go to step 20 or else go to step 23
- If rev%i==0, go to step 21 or else go to step 22
- k++. Go to step 22
- i++. Go to step 19
- If k==0, go to step 24 or else go to step 25
- true. Go to step 17
- false. Go to step 17
- Print "The given number is an Emirp Number". Go to step 28
- Print "The given number is not an Emirp Number".
- Stop
Program
Buffered Reader
import java.io.*;
class emirp
{
int n, temp, rev=0, digit, i, k;
void accept() throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
}
void reverse()
{
temp=n;
while(temp>0)
{
digit=temp%10;
rev=rev*10+digit;
temp=temp/10;
}
}
boolean isPrime()
{
k=0;
for(i=2;i<rev;i++)
{
if(rev%i==0)
{
k++;
}
}
if(k==0)
{
return true;
}
else
{
return false;
}
}
void display()
{
if(isPrime()==true && n!=rev)
{
System.out.println("The given number is Emirp Number");
}
else
{
System.out.println("The given number is not Emirp Number");
}
}
void main() throws IOException
{
accept();
reverse();
display();
}
}
Scanner
import java.util.*;
class emirp
{
int n, temp, rev=0, digit, i, k;
void accept()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void reverse()
{
temp=n;
while(temp>0)
{
digit=temp%10;
rev=rev*10+digit;
temp=temp/10;
}
}
boolean isPrime()
{
k=0;
for(i=2;i<rev;i++)
{
if(rev%i==0)
{
k++;
}
}
if(k==0)
{
return true;
}
else
{
return false;
}
}
void display()
{
if(isPrime()==true && n!=rev)
{
System.out.println("The given number is Emirp Number");
}
else
{
System.out.println("The given number is not Emirp Number");
}
}
void main()
{
accept();
reverse();
display();
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
int | n | To store number entered by the user. |
int | temp | To act as a temp variable |
int | digit | To store the digits of the number |
int | rev | To store the reverse of a number |
int | i | To act as a counter variable |
int | k | To store the number of factors of the number other than 1 and itself. |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept data from the user |
void | reverse() | To reverse the number |
boolean | isPrime() | To check if the reversed number is prime or not |
void | display() | To display whether the given number is emirp or not |
void | main() | To act as an entry gateway for the compiler |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Quora: ISC Java Code
Reddit: @ISCJavaCode
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment