Google+

Monday, December 30, 2013

Program to display CIRCULAR PRIME numbers..

This program is used to display the circular prime numbers within 100.

EXAMPLE:

 197,971,719..97,79..3..

PROGRAM :

package Blog;
/**
 * @author Raju
 */
public class cirprime {
   public static void main(String args[])
    {
        for(int number =1;number<100;number++){
             if(isCircularprime(number)){
              System.out.println(number);              
          }}
    }
    public static boolean isCircularprime(int number)
    {
        int i=1,count=0,length = (int)(Math.log10(number)+1);
        int temp = number;
        while(i<=length){
           if(isPrime(temp))
                    {
                      count++;  
                    }
           temp = rotate(number);
           i++;
        }
        if(count==length)
        {
            return true;
        }
        return false;
    }
    public static boolean isPrime(int number){
        for(int i=2; i<number; i++){
           if(number%i == 0){
               return false; //number is divisible so its not prime
           }
        }
        return true; //number is prime now
    }
    public static int rotate(int number)
    {
        int length = (int)(Math.log10(number)+1);       
        int rotate = number%10;
        rotate = rotate * (int) Math.pow(10, length-1);
        number = number/10;
        rotate = rotate + number;
        return rotate;
    }       

}


Don't forget to give your valuable comments..


No comments:

Post a Comment