Code with Vinay

ISC practical papers › ISC 2016 › Question 1

Circular Prime Number

ISC 2016 Computer Science Practical, Question 1 · Number programs

The 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

Question: ISC 2016 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2016Q1.java Download GitHub
import java.util.*;

class CircularPrime {
    private int N, copy, divisor;
    private boolean isPrime;

    public CircularPrime(int N) {
        this.N = N;
        copy=N;
        divisor=1;
        for( int temp=N; temp>9; temp/=10){
            divisor*=10;
        }
        isPrime=true;
    }

    private void shift(int n) {
        copy=(n%divisor)*10+(n/divisor);
    }

    

    public void display(){
        do{
            System.out.println("\t"+copy);
            if(!isPrime(copy)){
                isPrime=false;
            }
            shift(copy);
        }while(N!=copy);
        if(isPrime){
            System.out.println("\t"+N+" IS A CIRCULAR PRIME");
        }else{
            System.out.println("\t"+N+" IS NOT A CIRCULAR PRIME");
        }
    }
    private static boolean isPrime(int n) {
        if (n < 2)
            return false;
        else if (n == 2)
            return true;
        else if (n % 2 == 0)
            return false;
        else {
            // number is odd
            int limit = (int) Math.sqrt(n);
            for (int divisor = 3; divisor <= limit; divisor += 2) {
                if (n % divisor == 0)
                    return false;
            }
        }
        return true;
    }
}

class ISC2016Q1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\tN = ");
        int N=in.nextInt();
        CircularPrime obj = new CircularPrime(N);
        System.out.print("OUTPUT:");
        obj.display();
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers