Q:

Java program to check Emrip number

belongs to collection: Java Basic Programs

0

Given an integer number and we have to check whether it is Emrip number or not using Java program.

Example:

 

    Input: 13
    Output:
    13 is Emrip number
    Explanation:
    13 and its reverse is 31 both are primer numbers

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check Emrip number in java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class CheckEmirpNumber
{
	// create object and intialize here.
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	int n, rev, f;

	// create parameterised constructor
	CheckEmirpNumber(int nn) 
	{
		n=nn;
		rev=0;
		f=2;
	}

	// function for checking the prime number.
	int isprime(int x) 
	{
		if(f<=x)
		{
			if(x%f!=0) 
			{
				f++;
				isprime(x);
			}
		}

		if(f==x)
			return 1;
		else
			return 0;
	}

	// function to check Emrip number.
	void isEmirp()
	{
		int copy=n, d;
		
		// reversing the number.
		while(copy>0) 
		{
			d=copy%10;
			rev=rev*10+d;
			copy=copy/10;
		}
		
		
		int a=isprime(n); 
		f=2; 
		int b=isprime(rev); 
		
		// if both a and b are equal then it is Emrip
		if(a==1 && b==1)
			System.out.println(n+" is an Emirp Number");
		else
			System.out.println(n+" is Not an Emirp Number");
	}

	public static void main(String args[])throws IOException
	{
		// Enter the number
		System.out.print("Enter the number : ");
		
		int n=Integer.parseInt(br.readLine());
		
		// call the function to check.
		CheckEmirpNumber ob=new CheckEmirpNumber(n);
		ob.isEmirp();
	}    
}

Output

First run:
Enter any number : 13
13 is an Emirp Number

Second run:
Enter the number : 250
250 is Not an Emirp Number

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to count number of notes (rupees) in ... >>
<< Java program to find the correct output of student...