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

## Java Numbers: Exercise-17 with Solution

Write a Java program to check if a given number is a circular prime or not.

Circular Prime : A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will be prime.

For example, 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime. A circular prime with at least two digits can only consist of combinations of the digits 1, 3, 7 or 9, because having 0, 2, 4, 6 or 8 as the last digit makes the number divisible by 2, and having 0 or 5 as the last digit makes it divisible by 5.

*Test Data*

Input a number: 35

**Pictorial Presentation:**

**Sample Solution:**

**Java Code:**

```
import java.util.Scanner;
public class Example17 {
public static void main( String args[] ){
int num;
Scanner sc = new Scanner( System.in );
System.out.print("Input a number: ");
num = sc.nextInt();
int num_of_digits = 0, divisor_part=1, circular_num = num;
boolean allPrime = true;
for( int i = num; i > 0; i /= 10 ){
num_of_digits++;
divisor_part *=10;
}
divisor_part /=10;
do{
circular_num = circulate_func( circular_num, divisor_part );
if( !isPrime( circular_num ) ) allPrime=false;
}while( circular_num != num );
if( allPrime ) System.out.println("It is Circular Prime number." );
else System.out.println("It is not a Circular Prime number." );
}
public static boolean isPrime( int n ){
int factorCount = 0;
if( n < 2 ) return false;
else if( n == 2 ) return true;
else if( n % 2 == 0 ) return false;
else{
int num = (int) Math.sqrt( n );
for( int i = 3 ; i <= num; i+=2 ){
if( n %i == 0 ){
return false;
}
}
}
return true;
}
public static int circulate_func( int n, int divisor_part ){
if( n < 10 ) return n;
else return ( n % divisor_part ) * 10 + n / divisor_part;
}
}
```

Sample Output:

Input a number: 35 It is not a Circular Prime number.

**Flowchart: **

**Java Code Editor:**

** Contribute your code and comments through Disqus.**

**Previous:** Write a Java program to check two numbers are Amicable numbers or not.

**Next:** Write a Java program to check a number is a cube or not.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

## Java: Tips of the Day

**IsPowerOfTwo**

Checks if a value is positive power of two.

To understand how it works let's assume we made a call IsPowerOfTwo(4).

As value is greater than 0, so right side of the && operator will be evaluated.

The result of (~value + 1) is equal to value itself. ~100 + 001 => 011 + 001 => 100. This is equal to value.

The result of (value & value) is value. 100 & 100 => 100.

This will value the expression to true as value is equal to value.

public static boolean isPowerOfTwo(final int value) { return value > 0 && ((value & (~value + 1)) == value); }

Ref: https://bit.ly/3sA5d4I

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook