Write a Java Program to Check Circular Prime Number

In the previous article, we have discussed Java Program to Check Prime Number

In this article we are going to understand what Circular Prime number is and how we can check whether a number is Circular Prime or not in Java with examples.

Java Program to Check Circular Prime Number

Circular Prime numbers are prime numbers with the property that the number generated at each intermediate step when cyclically permuting its digits will be prime.

Example :

13-> Circular Prime number
17-> Circular Prime number
130 -> Not a Circular Prime number

In the above examples the number 13 and 17 are circular prime number.

Let’s see different ways to check circular prime number.

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Approach:

  1. We ask the user to enter a number which we store in an integer variable num.
  2. We then use a while loop to iterate and check for prime using the functions for all values.
  3. If flag is true, it is said to be a circular prime number or else it is not.

Method-1: Java Program to Check Circular Prime Number By Using Static Value

import java.util.Scanner;
public class CircularPrimeNumber
{
    public static void main(String args[])
    {
        //A number declared
        int num = 13;

        //Iter is the iterator which starts from 2 as 1 divided every number
        int iter = 0, temp = num, remainder;
        //Flag is used to record if the number is divisible after each iteration
        boolean flag = true;
        while (temp > 0)
        {
            remainder = temp % 10;
            iter++;
            temp = temp / 10;
        }
        temp = num;
        for (int i = 1; i <= iter; i++)
        {
            remainder = temp % 10;
            temp = temp / 10;
            temp = (remainder * (int) Math.pow(10, iter - 1)) + temp;
            if (!prime(temp))
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            System.out.println(num+" is circular prime");
        }
        else
        {
            System.out.println(num+" is not circular prime");
        }
    }

    // Function to check for prime
    static boolean prime(int n)
    {
        int i = 2;
        boolean flag = true;
        while (n > i)
        {
            if (n % 2 == 0)
            {
                flag = false;
                break;
            }
            i++;
        }
        return flag;
    }
}
    
Output:

13 is a circular prime

Method-2: Java Program to Check Circular Prime Number By Using User Input Value

import java.util.Scanner;
public class CircularPrimeNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        //Iter is the iterator which starts from 2 as 1 divided every number
        int iter = 0, temp = num, remainder;
        //Flag is used to record if the number is divisible after each iteration
        boolean flag = true;
        while (temp > 0)
        {
            remainder = temp % 10;
            iter++;
            temp = temp / 10;
        }
        temp = num;
        for (int i = 1; i <= iter; i++)
        {
            remainder = temp % 10;
            temp = temp / 10;
            temp = (remainder * (int) Math.pow(10, iter - 1)) + temp;
            if (!prime(temp))
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            System.out.println(num+" is circular prime.");
        }
        else
        {
            System.out.println(num+" is not circular prime.");
        }
    }

    // Function to check for prime
    static boolean prime(int n)
    {
        int i = 2;
        boolean flag = true;
        while (n > i)
        {
            if (n % 2 == 0)
            {
                flag = false;
                break;
            }
            i++;
        }
        return flag;
    }
}
    
Output:
Enter a number : 19
19 is circular prime

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: