Saturday, March 21, 2020

Circular Prime Number Program Using BlueJ


For Details of the program: CLICK HERE

Programming Code:
import java.io.*;
class CPrime
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void takeNumber()throws Exception
{
while(true)
{

System.out.println("Enter the Number (0 to exit):"); 
n=Integer.parseInt(br.readLine()); if(n==0) 
break;
if(isCircularPrime(n))

System.out.println(n+ " is a Circular Prime Number.");
 else

System.out.println(n+ " is not a Circular Prime Number.");

}
}
boolean isCircularPrime(int n)
{
int a,flag=1;

String s;
a=n;
System.out.println(a);
do
{
if(isPrime(a))
{
s=String.valueOf(a);
s=s.substring(1)+s.charAt(0);
a=Integer.parseInt(s);
System.out.println(a);
}
else
{
flag=0;
break;
}
}while(a!=n);
if(flag==1)
return true;
else
return false;
}
boolean isPrime(int n)

{
int i;
for(i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i==n)
return true;
else
return false;
}

public static void main(String args[]) throws Exception
{
CPrime ob=new CPrime();
ob.takeNumber();
}
}

Variable Description

Type
Name
Purpose
Scope
int
n
Holds the number
Within void


which will be
takeNumber ()


checked for circular
function


prime

BufferedReader
br
Used for user input
Within void



takeNumber ()



function


Algorithm

Step 1: int type variable ‘n’ and BufferedReader object ‘br’ are created.

Step 2: Take the number from user and store it in ‘n’

Step 3: The number is checked for prime and the digits of the number are rearranged by setting the extreme left position digit at the right to form another number until it becomes the original number

Step 4: If all the combinations are prime, display the number as circular prime otherwise not

Back To 2016 Computer Practical paper: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner