Java Program to find Emirp Number

Java program for Emirp Number

A number which is prime backward and forwards is called emirp number. Example: 13 and 31 are both prime numbers. Thus, 13 is an emirp number. Design a class Emirp to check if a given number is Emirp number or not. Some of the members of the class are given below:
Class name                 Emirp
Data members/instance variables:
n                               stores the number
reverse                     stores the reverse of the number
f                                stores the divisor
Member functions :
Emirp(int n)               to assign n = n, reverse = 0 and f = 2
int isprime(int x)         check if the number is prime using the recursive technique and return I if prime otherwise return 0
void isEmirp( )             reverse the given number and check if both the original number and the reverse number are prime. by invoking the function isprime(int) and display the result with an appropriate message
Specify the class Emirp giving details of the constructor(illt), int isprime(int) and void isEmirp().
Define the main( ) function to create an object and call the methods to check for Emirp number.

import java.io.IOException;
/**
* class Emirp
* ISC 2013 paper Question 08
*/
public class Emirp
{
// instance variables
private int n;      // For number
private int reverse;    // reverseeres number
private int temp;   // temporary variable

/**
* Constructor for objects of class Emirp
*/
public Emirp(int n)
{
// initialise instance variables
this.n = n;
reverse = 0;
temp = 2;
}

public int isPrime(int x, int i)
{

if(i==1){
return 1;
}
else
{
if(x%i==0)
return 0;
else
return isPrime(x,i-1);
}
}
void isEmirp()
{
int a,p1, p2;
temp = n;
while(n !=0 )
{
a = n % 10;
reverse = reverse * 10 +a;
n = n/10;
}
System.out.println(reverse);
p1 = isPrime(temp,temp/2);          // call the isPrime for the original number
p2 = isPrime(reverse,reverse/2);    // call the isPrime for the reverse number
if( p1 == 1 && p2  == 1)
{
System.out.println("Number is Emirp");
}
else
{
System.out.println("Number is not Emirp");
}
}
public static void main(String args[])throws IOException
{
int n;
System.out.println("Enter the number ");
Emirp ob = new Emirp(n);
ob.isEmirp();
}
}

Sample Output
Enter the number
13
31
Number is Emirp

Enter the number
14
41
Number is not Emirp

Enter the number
71
17
Number is Emirp
 Sl. No. Variable Name Data Type Purpose 1 n, reverse, temp int instance variables 2 Emirp() Constructor The constructor of Emirp class 3 isPrime(int, int) int Checking prime number or not (recursive method) 4 isEmirp() void Checking Emirp 5 a, p1, p2 int Local variable to isEmirp() method 6 in InputStreamReader The object of InputStreamReader class which exists in java.io package 7 br BufferedReader Use to create object br which exists in java.io package main() void Main function

More Program

Loops in Java - Java loop explained with examplesJava for loopArray in JavaTwin prime number in javacircular prime number program in java

Neon number program in java

Write a program in java to accept a number from the user and check if it is a neon number or not.

The neon number is nothing but the sum of digits of square of the number is equal to the number.
Example :
Input number 9
Square the number 9 * 9 and it comes to 81
Sum the digits of the square number 8 + 1 = 9
If the number is equal to the sum of the square number then it is a neon the number and if it is not then it is not a neon number.

Java program of Neon Number Example

import java.util.*;
// Neon Number Class
public class NeonNumberCheck
{
// main method begins execution of this class
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;      // n for number to check neon or not
int m;      // m for square of the n
int a;      // a for taking reminder
int s=0;    // for summation
System.out.println("Enter a number: ");
n=sc.nextInt();
m=n*n;      // Squaring of the number n
while(m!=0)
{
a=m%10; // Calculating reminder
s=s+a;  // adding the reminder to s
m=m/10; // Integer division to truncate the last digit
}
if(s == n)  // Checking the original number to the summation
{
System.out.println("Neon number");
}
else
{
System.out.println("Not a Neon Number");
}
}
}

Sample output
Enter a number:
8
Not a Neon Number
Enter a number:
9
Neon number
Enter a number:
10
Not a Neon Number

In the above program, the entered number is calculated as m = n*n and then 'm' is mode division by 10 and reminder added to s as s=s+a and then integer division is don till number become Zero.
After check it as
if(s == n)  // Checking the original number to the summation
{
System.out.println("Neon number");
}
else
{
System.out.println("Not a Neon Number");
}
This will give the required result.

Variable Description
 Sr. No Variable / Function Types Description 1 sc Object Object of Scanner class 2 main() void main() method 3 n int No to be check as NEON no. 4 m int Store n 5 a int For reminder 6 s int Summation calculation

More program

Java program for Evil Number

More Java program