CIRCULAR PRIME NUMBER | ISC COMPUTER 2024 | survnor.blogspot.com
A circular prime number that remains prime index
shifts of its digits. When the left most 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.e., 1 and itself. Example:- 131à113à131. Hence,
131 is a circular prime.
Accept a positive number ‘n’ and check whether it is a
circular prime or not. The new numbers formed after the shifting of the digits
should also be displayed.
ALGORITHM
STEP 1:- Start.
STEP 2:- Declare circular_prime as a class.
STEP 3:- Declare isprime as a method of
return type Boolean with formal parameter num. Declare and variables c and i
and initialize then with 0.
STEP 4:- Start iteration where the loop is
initialized by 1 on 'i' up to num and incremented by 1.now check whether num%i
is equal to 0 or not . if it is equal to 0 then c is incremented by 1 in each step.
STEP 5:- Return c==2 and close the method
isprime().
STEP 6:- Declare a method digitcount () of
return type int and with formal parameter num. Again initialize c by 0 . start
iteration till num!=0. Within the loop increment c by 1 and store the value of
num /10 in the variable num as long as the
condition is true.
STEP 7:- Return c and close the method digitcount ().
STEP 8:- Declare a method check with return
type void. Accept the user given integer and store it in variable n along with
the message "ENTER THE INTERGER TO CHECK". Now check whether n is
less than equal to zero or not, if so then print the message "INVALID
INPUT" and return the number.
STEP 9:- Initialize variable iscircularprime
of type Boolean by true. Now check whether the user given number is prime or
not by calling the above isprime() method, if so then print the number.
STEP
10:- Declare
variables dc and div. Now store the length of the user given number in dc by
calling the above method digitcount() and store the value of n in n2. Store the
value of 10 to the power (dc-1) in div. Declare i t1 and t2 as variables and
initialize t2 by 0.
STEP
11:- Start
iteration by initializing i by 1 where i continues upto less than dc and
increamented by 1. Now divide n2 by div and store the value in t1 and the
remainder in t2 and then multiply t2 with 10 and add t1 to it and finally store
it into n2.
STEP
12:- Now print n2
and check whether n2 is prime or not by again calling the isprime () method if
n2 is not prime then break the statement otherwise store false statement in
iscircularprime.
STEP
13:- Now finally
check whether iscircularprime is a circular prime number or not by using if
else statement. If it is circular prime then print the particular number along
with the message "IS A CIRCULAR PRIME" else print the number with the
message" IS NOT A CIRCULAR PRIME".
STEP
14:- Close the
method check().
STEP
15:- Now declare main() function with return type void
and create an object ob of class circular_prime and finally call the method
check() through object ob.
STEP
16:- Close the main() function and class circular_prime.
STEP
17:- Stop.
SOURCE CODE
import
java.util.*;
class
circular_prime{
public static boolean isprime(int num){
int c=0;
int i=0;
for(i=1;i<=num;i++){
if(num%i==0){
c++;}}
return c==2;}
public static int digitcount(int num){
int c=0;
while(num!=0){
c++;
num=num/10;}
return c; }
public static void check(){
Scanner sc=new Scanner(System.in);
System.out.println("ENTER INTEGER
TO CHECK:");
int n=sc.nextInt();
if(n<=0){
System.out.println("INVALID
INPUT");
return;}
boolean iscircularprime=true;
if(isprime(n)){
System.out.println(n);
int dc=digitcount(n);
int div=(int)(Math.pow(10,dc-1));
int n2=n;
int i,t1,t2=0;
for(i=1;i<dc;i++){
t1=n2/div;
t2=n2%div;
n2=t2*10+t1;
System.out.println(n2);
if(!isprime(n2)){
iscircularprime=false;
break;}}}
else{
iscircularprime=false;}
if(iscircularprime){
System.out.println(n+" IS A
CIRCULAR PRIME");}
else{
System.out.println(n+" IS A
NOT A CIRCULAR PRIME");}}
public static void main(){
circular_prime ob=new circular_prime();
ob.check();}}
VARIABLE DESCRIPTION
S.L.No. |
Name |
Data Type |
Description |
1 |
c |
int |
Used as a counter variable. |
2 |
i |
int |
Used as a loop control variable. |
3 |
num |
int |
Used as a formal parameter to
check whether the user given number is prime or not. |
4 |
n |
int |
To accept the user given number. |
5 |
dc |
int |
For storing the length of n. |
6 |
div |
int |
For storing the value of 10 to the
power(dc-1) and for dividing the values of n2. |
7 |
n2 |
int |
For storing the value of n and is used
for circulating the user given number. |
8 |
t1 |
int |
For storing the divided value of
n2. |
9 |
t2 |
int |
For storing the remainder of n2
after division for storing. |
10 |
iscircularprime |
|
To check whether the given number
is circular prime or not. |
INPUT AND
OUTPUT