Monday, 11 February 2019

PrimePal2012_1


import java.util.*;

class PrimePal2012_1
{
    public void showPrimePal()
    {
        Scanner sc = new Scanner(System.in);
        int m,n, c=0;
        System.out.println("Enter the Lower Limit:");
        m=sc.nextInt();
        System.out.println("Enter the Upper Limit:");
        n=sc.nextInt();
        if(m>n || n>3000)
            System.out.println("Out of Range.");
        else
        {
            System.out.println("The Prime Palindrome integers are:");
            while(m<=n)
            {
                if(checkPrimePal(m))
                {
                    if(c==0)
                        System.out.print(m);
                    else
                        System.out.print(", "+m);
                    c++;
                }
                m++;
            }
            System.out.println("\nFrequency of Prime Palindrome integers: "+c);
        }
    }
    boolean checkPrimePal(int x)
    {
        boolean flag=false;
        int c=0, temp=x;
        int rev=0,rem=0;
        for(int i=1;i<=x;i++)
        {
            if(x%i==0)
                c++;
        }
        if(c<=2)
        {
            while(x!=0)
            {
                rem=x%10;
                rev=rev*10+rem;
                x/=10;
            }
            if(rev==temp)
                flag=true;
        }
        return flag;
    }
    public static void main(String args[])
    {
        PrimePal2012_1 ob=new PrimePal2012_1 ();
        ob.showPrimePal();
    }
}

No comments:

Post a Comment