JAVA Practical Question ISC 2016 Solve - Circular Prime
09-08-2019    252 times This program is selected from ISC 2016 Computer Science or Class 12 practical paper.

A Circular prime is a prime number that remains a prime under cyclic shifts of its digits. When left most digits is removed and replaced at the end of the remaining string of the 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.

Examples 1:

• 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 number formed after the shifting of the digits should also be displayed.

import java.util.*;
public class CircularPrime
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
CircularPrime cp=new CircularPrime();
Boolean flag=true;
System.out.print("Enter A Number : ");
int noi=sc.nextInt();
String nos=String.valueOf(noi);
int len=nos.length();

for(int i=1;i<=len;i++)
{
System.out.println(nos);
if(!cp.IsPrime(nos))
{
flag=false;

}
nos=cp.changeNumber(nos);
}
if(flag)
{
System.out.print(noi + " is a Circular Prime No");
}
else
{
System.out.print(noi + " is not a Circular Prime No");
}
}
String changeNumber(String n)
{
String s="";
s=n.substring(1)+n.substring(0,1);
return s;
}
boolean IsPrime(String n)
{
int no=Integer.parseInt(n);
boolean b=true;
int d=2;
while(d<=Math.sqrt(no))
{
if(no%d==0)
{
b=false;
break;
}
d++;
}
return b;
}
}


Test with the no 1193
Enter A Number : 197
197
971
719
197 is a Circular Prime No

Test with the no 131
Enter A Number : 131
131
311
113
131 is a Circular Prime No

Test with the no 1193
Enter A Number : 1193
1193
1931
9311
3119
1193 is a Circular Prime No

Test with the no 29
Enter A Number : 29
29
92
29 is not a Circular Prime No 