Circular Prime Number Program in Java (ICSE & ISC BlueJ)

⚡ Direct Answer & Key Takeaway

A Circular Prime is a positive integer greater than 1 that remains prime under every cyclic rotation of its digits. For example, 1193 produces the rotations 1193, 1931, 9311, and 3119, all of which are prime numbers. In ICSE and ISC Java exams, you isolate the first digit using integer division by 10^(d-1), shift the remaining digits left by multiplying by 10, append that leading digit, and test primality at each rotation.

1. How Cyclic Digit Rotation Works Without Strings

Although string concatenation looks tempting, CISCE examiners award full logic marks when cyclic rotation relies on pure integer arithmetic. Suppose the input number is 197, which contains three digits. You first compute the base power as 10^(3 - 1) = 100. Dividing 197 by 100 extracts the leftmost digit 1, while 197 % 100 preserves the trailing digits 97. Multiplying 97 by 10 and adding 1 forms the next rotation, 971. Repeating this shift exactly d times—where d is the digit count—cycles through every arrangement and cleanly returns to the original number.

  • Count total digits d using a temporary copy so the original value stays intact for printing.
  • Compute divisor = (int) Math.pow(10, d - 1) once before the rotation loop starts.
  • Rotate with rot = (rot % divisor) * 10 + (rot / divisor) to shift the leftmost digit right.
  • Reject inputs <= 0 upfront to prevent infinite or invalid division behavior.

2. Optimizing the Primality Check Inside a Helper Method

Writing a dedicated boolean method isPrime(int x) keeps your Section B solution modular and prevents nested-loop confusion during dry runs. Inside isPrime(x), return false immediately for any integer less than 2, because 1 is neither prime nor composite. Next, test divisors from 2 up to the square root of x instead of x - 1. If any divisor divides x with zero remainder, return false right away. Using a method also lets you keep the rotation loop concise and easy for examiners to verify in BlueJ.

  • Guard x < 2 first so single-digit edge inputs like 0 or 1 never pass as prime.
  • Use i * i <= x inside isPrime() for efficient O(sqrt(n)) factor checking without floating-point rounding.
  • Maintain a boolean flag isCircular = true and flip it to false whenever any rotation fails isPrime().

3. Board-Exam Edge Cases: Single Digits, Zeros, and 1193

Board practical questions frequently test whether your code handles single-digit primes and numbers containing zeros properly. Single-digit primes such as 2, 3, 5, and 7 have only one rotation and qualify as Circular Primes. Conversely, 19 is prime, yet its rotation 91 equals 7 * 13, so it fails. Watch out for numbers with internal zeros like 101: rotating 101 with pure integer division turns 101 into 11 (since leading zero vanishes in integer 011), whereas string-based rotation keeps "011". Most ISC papers ask to print every rotation step before stating the verdict.

  • Print each rotation inside the loop when the question asks to display all cyclic permutations.
  • Recognize the distinction between 101 (loses leading zero in integer arithmetic) and 1193 (all digits nonzero).
  • Validate negative inputs explicitly with an Invalid Input message before entering the digit-count loop.

4. CircularPrime.java (BlueJ / CISCE Exam Format)

import java.util.*;

public class CircularPrime {
    static boolean isPrime(int x) {
        if (x < 2) {
            return false;
        }
        for (int i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = sc.nextInt();

        if (n <= 0) {
            System.out.println("Invalid Input");
            sc.close();
            return;
        }

        int temp = n;
        int digits = 0;
        while (temp > 0) {
            digits++;
            temp /= 10;
        }

        int divisor = (int) Math.pow(10, digits - 1);
        int rot = n;
        boolean isCircular = true;

        for (int i = 1; i <= digits; i++) {
            System.out.println(rot);
            if (!isPrime(rot)) {
                isCircular = false;
            }
            int firstDigit = rot / divisor;
            int rem = rot % divisor;
            rot = rem * 10 + firstDigit;
        }

        if (isCircular) {
            System.out.println(n + " IS A CIRCULAR PRIME");
        } else {
            System.out.println(n + " IS NOT A CIRCULAR PRIME");
        }
        sc.close();
    }
}

Verified Console / Dry-Run Output:

Enter a positive integer: 1193
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME

Enter a positive integer: 29
29
92
29 IS NOT A CIRCULAR PRIME

5. CISCE Variable Description Table (VDT) & Dry Run

VariableData TypeExam Purpose & Sample Value
nintStores user input integer to test (e.g., 1193)
digitsintTotal digit count of n computed via temp loop (e.g., 4)
divisorintPower of 10 splitting leftmost digit: 10^(digits-1) (e.g., 1000)
rotintHolds each cyclic rotation in sequence (1193 -> 1931 -> 9311 -> 3119)
isCircularbooleanFlag tracking whether every rotation passes isPrime() (true)
iintLoop counter from 1 to digits and trial divisor inside isPrime()
🎯 1-on-1 Live Mentorship & Free Study Kit

Master Circular Prime & Number Logic 1-on-1

Book a 1-on-1 ICSE & ISC BlueJ live session to trace cyclic rotations, dry-run tricky 4-digit edge cases, and lock in full Section B marks.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Reversing digits or generating random permutations instead of shifting the leftmost digit to the units place cyclically.
  • Avoid Mistake: Forgetting to guard x < 2 inside isPrime(), which falsely classifies 1 as a prime rotation.
  • Avoid Mistake: Mutating n directly while counting digits so the final output prints 0 instead of the original number.
  • Examiner / Mentor Tip: Always print every rotation on its own line if the ISC/ICSE question asks to display cyclic permutations before the final result.
  • Examiner / Mentor Tip: Cast Math.pow(10, digits - 1) explicitly to (int) because Math.pow returns a double in Java.
  • Examiner / Mentor Tip: Include both the rotation loop variables (rot, divisor) and helper method variables in your CISCE Variable Description Table.

7. Interactive Concept Check Quiz

Question: What is the sequence of cyclic rotations generated for n = 197 when moving the leftmost digit to the units place on each step?

8. Frequently Asked Questions

Is 1193 a Circular Prime, and how many rotations does it need?
Yes, 1193 is one of the classic four-digit Circular Primes featured in ISC Computer Science practicals. Because it has four digits, the program must generate and verify exactly four cyclic rotations: 1193, 1931, 9311, and 3119. Every one of these four numbers has no factors other than 1 and itself, so the flag stays true.
Why do ICSE teachers prefer integer math over String rotation for Circular Prime?
CISCE marking schemes value pure integer arithmetic because it tests mastery of /, %, and Math.pow(10, d - 1) without relying on wrapper parsing like Integer.parseInt(). Integer rotation also runs faster in memory and reinforces digit-manipulation patterns reused in Automorphic, Kaprekar, and Disarium problems across Class 10 and ISC papers.
Is a single-digit prime like 7 considered a Circular Prime in Java exams?
Yes, single-digit primes—namely 2, 3, 5, and 7—are valid Circular Primes because they have only one digit and therefore only one rotation, which is prime. Meanwhile, 1 is never a Circular Prime because 1 is neither prime nor composite, so your isPrime() helper must return false whenever the candidate value is below 2.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class