# Circular Prime in JAVA

Hello guys, if you are looking for a tutorial which would help you to determine if a number inputted by the user is **Circular Prime or Not in Java**, then you came to the right place. In this tutorial I will explain the solution through programming in java, but first let us take a look at what a Circular prime number is.

A number can be referred to as circular prime only if the number generated at each intermediate step when cyclically permuting its (base 10) digits will be prime. For example :

- 113
- 311
- 131

Since all these three numbers are prime, they can be referred as Circular Prime.

Also read:

- How to find the nth prime number in Java
- Find whether a number is a twisted prime or not between 1 to 100 in Java

## Check if a number is a circular prime or not in Java:

import java.util.*; class circularprime { String strn;int i,l,div=1,rem,copyno,n,rtn; Scanner sc=new Scanner(System.in); public void main() { System.out.println("enter the number"); n=sc.nextInt(); strn=Integer.toString(n); l=strn.length(); for(i=1;i<l;i++) { div=div*10;// to store multiple of 10,to divide } rtn=compute(n,l); rtn=ifprime(n); } public int ifprime(int n) {int count=0; for(i=1;i<=n;i++) { if(n%i==0) count++; } if(count==2) System.out.println("Circular prime"); else System.out.println("Not-Circular Prime"); return 0; } public int compute(int num,int l) {int i=0; while(i<l-1) { rem=n%10; n=n/10; n=(rem*div)+n; System.out.println(n); i++; } return 0; } }

#### Output :

enter the number 113 311 131 Circular prime

##### Explanation :

The program begins by importing util package, which would help us by enabling scanner class. We then Set the class, and declare Global variables. In addition to the Main method, we have the ‘ifprime’ method which would help us determine if the number is prime or not, and compute which would display all the permuted values for of the number inputted.

In the main method, the user is first asked to enter a number, which is converted in a string format, to get the length of the number, which would help us determine how many digits it has. We then create the counter ‘div’ which would help us to keep the digits in their respective position. The next step is calling the other two methods, which would print out all the permuted values of the number; and check if the number is prime respectively.

That’s cool….Thanks a lot