Programming Examples

Java program for Circular Prime Number


A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost 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 and itself.

Write a program to accept one no check the number where it is Circular Prime or Not.

Example 1

INPUT :N = 197

OUTPUT: 197 971 719 197 IS A CIRCULAR PRIME

Example 2

INPUT :N = 1193

OUTPUT: 1193 1931 9311 3119 1193 IS A CIRCULAR PRIME

Example 3

INPUT :N = 29

OUTPUT: 29 92 29 IS NOT A CIRCULAR PRIME

Solution 1 :

import java.util.*;
class CircularPrime
{
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        int i,j,n,p,r1,r2,c=0,f=0;
        System.out.println("Enter an Number ");
        n=sc.nextInt();
        System.out.println("The different Combination of Prime number s are:");
        p=n;c=0;
        while(p>0)
        {
            p=p/10;
            c++;
        }
        p=n;
        outer:
        for(i=1;i<=c;i++)
        {
            f=1;
            for(j=2;j

Solution 2:

import java.util.*;
class CircularPrime
{
    static boolean isprime(int num)
    {
        boolean flag=true;
        for(int a=2;a<=num/2;a++)
        {
            if(num%a==0)
            {
                flag=false;
                break;
            }
        }
        return(flag);
    }
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        int num,count=0,temp,base;
        System.out.println("Enter any Number");
        num=sc.nextInt();
        temp=num;
        while(temp>0)
        {
            count++;
            temp=temp/10;
        }
        base=(int)Math.pow(10,count-1);
        boolean flag=true;
        for(int a=1;a<=count;a++)
        {
            num=(num%base)*10+(num/base);
            if(CircularPrime.isprime(num)==false)
            {
                flag=false;
                break;
            }
            System.out.println(num);
        }
        if(flag==true)
        {
            System.out.println("Number is Circular Prime");
        }
        else
        {
            System.out.println("Number is Not Circular Prime");
        }
    }
}
Output

Enter a number :
113
Output
113
131
311
Circular Prime
--------------------------------------------------------
Enter a number :
1193
Output
1193
1931
9311
3119
Circular Prime
-------------------------------------------------
Enter a number :
44
Output
Not a Circular Number
CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz