In this tutorial you will learn about the Program to check Circular Prime Program in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Circular Prime Program in Java Java
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- Function in java.
Circular Prime Number.
Prime number is called a circular prime if all its cyclic permutations digits,remains a prime number.
Examples:
1 2 3 |
number = 113 its all cyclic permutations of 113 (311 and 131) are prime. |
Program to check Circular Prime Program in Java
In this program we would find the given number is a Circular Prime number or not , first of all we will take the input from the user and check whether it is Circular Prime number or not. Lets have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.util.*; class CPrime { static boolean prime(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 no,ct=0,temp,base; System.out.println("Enter any Prime Number"); no=sc.nextInt(); temp=no; while(temp>0) { ct++; temp=temp/10; } base=(int)Math.pow(10,ct-1); boolean count=true; for(int j=1;j<=ct;j++) { no=(no%base)*10+(no/base); if(CPrime.prime(no)==false) { count=false; break; } System.out.println(no); } if(count==true) { System.out.println("Number is Circular Prime"); } else { System.out.println("Number is Not Circular Prime"); } } } |
Output
Circular prime number
Not a Circular prime number
In the above program, we have first declared and initialized a set variables required in the program.
- no,num= it will hold entered number.
- temp = it will hold temp value
- sum= for adding digit sum.
- count,ct= it will hold flag value.
- i,j= for iteration.
After declaring variables first we take a prime number from user.
then we check all the possible combination of a given number and all are prime number or not.
Here we created a function prime with in the class Cprime where we checked each and every possible combination of given number and all are prime or not and if they are prime.
A Circular Prime number is a prime number that remains prime under each every cyclic shifts of its digits.