**Click here to download the complete ISC 2016 Computer Science Paper 2 (Practical).**

**Question:**

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 I and itself.

**Example:**

131

311

113

Hence, 131 is a circular prime.

Test your program with the sample 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

**Programming Code:**

/** * The class CircularPrime_Q1_ISC2016 inputs a number and * checks whether it is a Circular Prime or not * @author : www.guideforschool.com * @Program Type : BlueJ Program - Java * @Question Year : ISC Practical 2016 Question 1 */ import java.util.*; class CircularPrime_Q1_ISC2016 { boolean isPrime(int n) // Function for checking whether a number is prime or not { int c = 0; for(int i = 1; i<=n; i++) { if(n%i == 0) c++; } if(c == 2) return true; else return false; } int circulate(int n) //Function for circulating the digits to form new number { String s = Integer.toString(n); String p = s.substring(1)+s.charAt(0); int a = Integer.parseInt(p); return a; } void isCircularPrime(int n) //Function to check for circular prime { int f = 0,a = n; do { System.out.println(a); if(isPrime(a)==false) { f = 1; break; } a = circulate(a); }while(a!=n); if(f==1) System.out.println(n+" IS NOT A CIRCULAR PRIME"); else System.out.println(n+" IS A CIRCULAR PRIME"); } public static void main(String args[]) { CircularPrime_Q1_ISC2016 ob = new CircularPrime_Q1_ISC2016(); Scanner sc = new Scanner(System.in); System.out.print("Enter a number : "); int n = sc.nextInt(); ob.isCircularPrime(n); } }

**Output:**

Enter a number : 87

87

87 IS NOT A CIRCULAR PRIME

Enter a number : 1193

1193

1931

9311

3119

1193 IS A CIRCULAR PRIME

Enter a number : 123

123

231

312

123 IS NOT A CIRCULAR PRIME

Bug found! If the number specified is 1310 the while loop never breaks since the number circulates to become 131. Thus a break statement is needed after f=1;(Line 42). I do not mean to criticize, this website does help me a lot! Please do make the above changes for a flawless program.

Thank you for pointing out the typo.

Sir, thank you very much … your programs have made me quite confident about string to othwr data type conversions.. i attempted the same program.. firat used pencil just like you told and then then compuled this program.. added comments.. wriiten algo and also gave a variable description table.. it compiled in a single go and i was the first one to lead out for viva .. but i couldn’t answer only 1 question.. do you think it will stop me from scoring a 30?