icse-promo

Question

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 1 and itself.

				
					Example:
131
311
113
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.A number is said to be prime if it has only two factors 1 and itself.

				
					Test your program with the following data and some random data:
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.
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class CircularPrimeNumber
{
    public static boolean isPrime(int num)
    {
        int count = 0;
        for(int i =1; i <= num; i++)
        {
            if(num % i == 0)
            {
                count++;
            }
        }
        if(count==2)
        {
            return true;
        }
        else
        {   
            return false;
        }
    }

    public static void main(String args[])
    {
        int newNum=0;
        String s="";
        Scanner sc=new Scanner(System.in);
        System.out.print("N = ");
        int n = sc.nextInt();
        boolean flag = true;
        newNum=n;
        do
        {
            if(isPrime(newNum)==true)
            {
                System.out.println(newNum);
                s = Integer.toString(newNum);
                s = s.substring(1) + s.charAt(0);
                newNum = Integer.parseInt(s);
                s="";
            }
            else
            {
                flag = false;
                break;
            }

        }while(newNum!=n);
        if(flag==true)
        {
            System.out.println(n + " IS A CIRCULAR PRIME.");
        }
        else
        {
            System.out.println(n + " IS NOT A CIRCULAR PRIME.");
        }
    }

}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *