PROGRAM : CYCLIC || CIRCULAR PRIME
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.
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.
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
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME
Example 2
INPUT :N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME
SYNTAX:
import java.io.*;class cyclic
{ static int c,c1,c2,count;//for counting no.of digits
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Number");
int n=Integer.parseInt(br.readLine());
int m=n;int d=0;
while(n!=0)
{d=n%10;
c++;
n=n/10;
}
c1=c;
c2=c1;
System.out.println("POSSIBLE OUTCOMES ARE: ");
outcomes(m);
}
public static void outcomes(int n)
{
int sum=0;int d=0;
d=n%10;
sum+=Math.pow(10,c1-1)*d+(n/10);
n=n/10;System.out.println(sum);
c--;pass(sum,c);
if(c>0)
{ c1=c2;
outcomes(sum);
}}
public static void pass(int n,int f)
{int x=0;int z=0;
for(int i=2;i<n;i++)
{if(n%i==0)
{ System.out.println("it have factor "+i);x++;
}
}
if(x==0)
count++;
if(f==0)
check();
}
public static void check()
{
if(count==c2)
System.out.println("YES THE INPUT IS CYCLIC PRIME");
else
System.out.println("NO THE INPUT IS NOT CYCLIC PRIME");
}
}
Comments
Post a Comment