Emrip Number

 //Write a program to accept a number and check it is an emrip number or not.

//A number is said to be an emrip if the number is prime backward and forward.

//example-13 and 31 both are prime

import java.util.*;

class emirp

{//class emirp opens

    int n=0;//variable n to accept the no

    boolean isprime(int n)//method to check whether the no is prime or not

    {

        int c=0;

        for(int i=1;i<=n;i++)

        {

            if(n%i==0)

            c=c+1;

        }

        if(c==2)

        return true;

        else

        return false;

    }

    void calculate(int k)//method to check whether both the actual and reverse nos are prime or not 

    {

        int r=0,rev=0,a=k;

        while(k>0)

        {

            r=k%10;

            k=k/10;

            rev=rev*10+r;//91

        }

        if(isprime(a)==true && isprime(rev)==true)

        System.out.println("It is an emirp no");

        else

        System.out.println("It is not an emirp no");

    }

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//13

        int k=n;//13

        emirp obj=new emirp();

        obj.calculate(k);

    }//main method closes

}//class closes


No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...