Circular prime in Java
In this post, you will learn the circular prime number program using the Java programming language.
A prime number is a whole number greater than 1, whose only factors are 1 and itself, like 2, 3, 5, 7, 11, etc.
A number is said to be a circular prime if all its possible rotations are also prime numbers. When the leftmost digit is taken out 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. The first few circular primes are 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 197,...
Example1:
INPUT: 97
OUTPUT: 97 79
All the numbers are prime so it is a circular prime number.
Example2:
INPUT: 113
OUTPUT: 113 311 131
All the numbers are prime so it is a circular prime number.
Algorithm of Circular Prime
- First, partition the number into its digits.
- Make all possible rotations with those digits.
- Then, check whether or not each possible rotation is a prime number.
Circular prime program in Java
In the given Java program, we accept a positive number n and check whether it is a circular prime or not. For this, we have created a function isPrime() which is in the class CircularPrime where we check each and every possible combination of a given number to see if they are prime or not.
import java.util.*;
class CircularPrime
{
static boolean isPrime(int num)
{
boolean count=true;
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
{
count=false;
break;
}
}
return(count);
}
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
int n,count=0,temp,base;
System.out.println("Enter any Prime Number: ");
n=sc.nextInt();
temp=n;
while(temp>0)
{
count++;
temp=temp/10;
}
base=(int)Math.pow(10,count-1);
boolean flag=true;
for(int j=1;j<=count;j++)
{
n=(n%base)*10+(n/base);
if(CircularPrime.isPrime(n)==false)
{
flag=false;
break;
}
System.out.println(n);
}
if(flag==true)
{
System.out.println("Number is circular prime.");
}
else
{
System.out.println("Number is not circular prime.");
}
}
}
Output of the above code:
Enter any Prime Number:
1193
1931
9311
3119
1193
Number is circular prime.
Enter any Prime Number:
113
131
311
113
Number is circular prime.
Enter any Prime Number:
73
37
73
Number is circular prime.
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java