×

Java Programs

Java Practice

Java program to check Emrip number

Here, we are implementing a java program that will read a number and check whether it is Emrip number or not.
Submitted by Chandra Shekhar, on January 08, 2018

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

Emrip Number

A Number is said to be an Emrip number if it is Prime from both the side backward and from forward.

Example:

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

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

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.