A **Circular Prime** is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is repeated until the original number is reached again.

A number is said to be prime if it has only two factors 1 and itself.

**Example:**

131

311

113

Hence, 131 is a circular prime.

Accept a positive number N and check whether it is a circular prime or not. The new numbers formed after the shifting of the digits should also be displayed.

Test your program with the following data and some random data:

**Example 1:****INPUT:**

N = 197**OUTPUT:**

197

971

719

197 IS A CIRCULAR PRIME.

**Example 2:****INPUT:**

N = 1193**OUTPUT:**

1193

1931

9311

3119

1193 IS A CIRCULAR PRIME.

**Example 3:****INPUT:**

N = 29**OUTPUT:**

29

92

29 IS NOT A CIRCULAR PRIME.

```
import java.io.*;
class CircularPrime{
public static void main(String args[])
throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("N = ");
int n = Integer.parseInt(br.readLine());
String s = Integer.toString(n);
int len = s.length();
boolean status = true;
for(int i = 1; i <= len; i++){
if(!isPrime(n))
status = false;
System.out.println(n);
s = s.substring(1) + s.charAt(0);
n = Integer.parseInt(s);
}
if(status)
System.out.println(n + " IS A CIRCULAR PRIME.");
else
System.out.println(n + " IS NOT A CIRCULAR PRIME.");
}
public static boolean isPrime(int num){
int f = 0;
for(int i = 1; i <= num; i++)
if(num % i == 0)
f++;
return (f == 2);
}
}
```

**Algorithm**:

1. INPUT N

2. `S = N AS STRING`

3. LEN = Length of N

4. STATUS = TRUE

5. REPEAT 1 TO LEN:

IF N IS NOT PRIME THEN STATUS = FALSE

PRINT N
S = SUB-STRING OF S FROM 2ND CHAR + 1ST CHAR

N = S AS INTEGER

6. IF STATUS = TRUE THEN PRINT "CIRCULAR" ELSE PRINT "NOT CIRCULAR"

## 20 replies on “Circular Prime ISC 2016 Practical”

Sir algorithm please

Algorithm is now added.

Sir,can we use char ch=(char)br.read(); in BufferedReader or in DataInputStream class?

You may use it, but sometimes it causes problems. It is safer to use br.readLine().charAt(0);

What is the procedure to input a character in BufferedReader or DataInputStream?

char ch = br.readLine().charAt(0);

Sir,how to arrange each column of a m x n matrix in matrix in ascending order in java?How to write the code in java?where m=no.of rows,n=no.of columns.

Sample input:

M = 3

N = 3

Original Matrix:

22 5 19

7 36 12

9 13 6

Matrix after sorting columns:

7 5 6

9 13 12

22 36 19

Each column must be sorted separately using bubble sort or selection sort technique. I will soon be posting a program on this.

Here is the link for your program.

Sir,How to delete an element from an array of n elements from a particular position? How to write the program code in java

I will soon be posting a program on this.

Sir,a program is given which states that:

A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is repeated until the original number is reached again.

A number is said to be prime if it has only two factors 1 and itself.

Example:

131

311

113

Hence, 131 is a circular prime.

Accept a positive number N and check whether it is a circular prime or not. The new numbers formed after the shifting of the digits should also be displayed.(Using Scanner class)

This sum was already solved. Click here to view the solution.

Here is the link for your program:

https://www.happycompiler.com/delete-element-array/

Sir,how to delete an element from an array of n elements

The size of an array is always fixed. No changes can be made in the original array by deleting or adding elements into it. To do so, you need to create a new array with a new size and copy all the elements to it from the old array every time you add or delete elements from that old array.

Can scanner class file operations be written in examinations

Yes

Can I use scanner class for the file program named marks.txt in the percentage mark program

No. Using Scanner class for file operations is not in syllabus.