Circular Prime Number

 //Write a program to accept a numbder and check it is a circular prime or not.

//A circular prime is a prime number with the property that the number generated at each intermediate

//step when cyclically permuting its digits will be prime.

//example= 113 is a circular prime number. 113,131,311 is circular prime

//1193,1931,9311,3119

//explanation left thru dry run

import java.util.*;

class circularp

{//class circularp opens

    int n;

    void accept()//method to accept the no

    {

        Scanner sc=new Scanner(System.in);

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

        n=sc.nextInt();//113

    }

    boolean isprime(int x)//method to check a no is prime or not//113

    {

        int c=0;

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

        {

            if(x%i==0)

            {

                c=c+1;

            }

        }

        if(c==2)

        return true;

        else 

        return false;

    }

    int getnext(int x)//method to generate the next no //113

    {

        int temp=x;//113

        int count=0;

        while(temp>0)

        {

            count++;//3

            temp=temp/10;

        }

        int num=x;//113

        int rem=num%10;//113%10=3

        int div=num/10;//113/10=11

        num=(int)((Math.pow(10,count-1))*rem)+div;//311

        return num;

    }

    boolean iscprime()//method for checking  no is circular prime or not

    

    {

        int nxt=n;//113

    while(isprime(nxt))//true

    {

        nxt=getnext(nxt);//311

        if(nxt==n)//311==113 f

        return true;

    }

    return false;

}

void display()//method to display the result

{

    if(iscprime()==true)

    System.out.println(n+" is a circular prime no");

    else

    System.out.println(n+" is not a circular prime no");

}

public static void main(String[]args)

{//main method opens

    circularp obj=new circularp();

    obj.accept();

    obj.display();

}//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 ...