Home » Java programs » Java basic programs

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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.