circular prime number program in java

Java Program for Circular Prime

According to Wikipedia, A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will be prime.

Examples : 113, 197, 199, 337, 1193, 3779, 11939, 19937 etc

Say 113 -> 131 -> 311 is circular prime.

Solution

import java.util.*;

public class CircularPrime

{

int n, m, c, f, i, a, b, j, f1; // Instance Variables

void circularPrime(int x)

{

c=0; // Total Digit Calculate

n=x;

f=0;

while(n!=0)

{

n/=10;

c++;

}

n=x;

System.out.println(“Output”);

System.out.println(“======”);

for(i=0;i<c;i++)

{

f=isPrime(n); // Returing 1 if Prime or 0 if not prime

if(f==1)

System.out.println(n);

else

{

System.out.println(“Not a Circular Number”);

break;

}

// Example 113 113 -> 131 -> 311

a=n%((int)Math.pow(10,c-1)); // Reminder 13

b=n/((int)Math.pow(10,c-1)); // quotient 1

m=a*10+b; // here m = 13 * 10 + 1 =131

n=m; // then n = 131 this process repeated till the loop end

}

if(f==1)

System.out.println(“Circular Prime”);

}

// Prime checking method which return 1 if prime else 0

int isPrime(int x)

{

f1=1;

for(j=2;j<x;j++)

{

if(x%j==0)

{

f1=0;

break;

}

}

return f1;

}

// Main method

public static void main(String args[])

{

int n,m,c,f,i;

Scanner sc = new Scanner(System.in);

System.out.println(“Enter a number :”);

n= sc.nextInt();

CircularPrime ob=new CircularPrime();

ob.circularPrime(n);

}

}

Sample output

Enter a number :

113

Output

======

113

131

311

Circular Prime

Enter a number :

137

Output

======

137

Not a Circular Number

In the above program, I have used two used to define methods, namely circularPrime() and isPrime(). The circularPrime(int x) takes a value and first it find out the number of digits in the given number. After then isPrime(n) is checking number is prime or not and it checks again after interchanging the number in this fashion 113 -> 131 -> 311

More Program

Java program for Pronic Number

Java Program to find Emirp Number

Java program for Evil Number

Originally published at https://javaknowhow.blogspot.com.

--

--

--

Computer Science and Digital Marketing Trainer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Sending Email via Flask app to Gmail

8 Tips For Remote Technical Assessments

Ready to start screen sharing

$25,000 Upbit Creepts Tournament + Play & Earn Contest

PHPUnit worst practices

5 Workflow Automation Use Cases You Might Not Have Thought Of

How to Check if a Number Is Prime in Python

5 must-have command line Kubernetes tools

GO: stack and heap allocation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Khurshid Md Anwar

Khurshid Md Anwar

Computer Science and Digital Marketing Trainer

More from Medium

Introduction to Java

Introduction to Java

Why is there a String array parameter in Java main method?