Skip to main content

CIRCULAR/CYCLIC PRIME

PROGRAM : CYCLIC || CIRCULAR PRIME

Question:

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.
Example:
131
311
113
Hence, 131 is a circular prime.
Test your program with the sample 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


SYNTAX:

import java.io.*;
class cyclic
{ static int c,c1,c2,count;//for counting no.of digits
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a Number");
        int n=Integer.parseInt(br.readLine());
        int m=n;int d=0;
        while(n!=0)
        {d=n%10;
            c++;
            n=n/10;
        }
            c1=c;
            c2=c1;
            System.out.println("POSSIBLE OUTCOMES ARE: ");
        outcomes(m);
    }
    public static void outcomes(int n)
    {
        int sum=0;int d=0;
            d=n%10;
            sum+=Math.pow(10,c1-1)*d+(n/10);
            n=n/10;System.out.println(sum);
            c--;pass(sum,c);
            if(c>0)
            { c1=c2;
            outcomes(sum);
        }}
        public static void pass(int n,int f)
        {int x=0;int z=0;
            for(int i=2;i<n;i++)
            {if(n%i==0)
                { System.out.println("it have factor "+i);x++;
                }
        }
        if(x==0)
            count++;
             
            if(f==0)
            check();
        }
    

public static void check()
{
    if(count==c2)
    System.out.println("YES THE INPUT IS CYCLIC PRIME");
    else
    System.out.println("NO THE INPUT IS NOT CYCLIC PRIME");
}
}






Comments

Popular posts from this blog

Arranging according to potential.

PROGRAM: Requested by Sir  Ankur Acharya   ARRANGING WORDS IN A STRING ACCORDING TO THEIR POTENTIAL The encryption of alphabets are to be done as follows: A = 1 B = 2 C = 3 . . . Z = 26 The potential of a word is found by adding the encrypted value of the alphabets. Example: KITE Potential = 11 + 9 + 20 + 5 = 45 Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order. Output the result in format given below: Example 1 INPUT : THE SKY IS THE LIMIT. POTENTIAL : THE = 33 SKY = 55 IS = 28 THE = 33 LIMIT = 63 OUTPUT : IS THE THE SKY LIMIT Example 2 INPUT : LOOK BEFORE YOU LEAP. POTENTIAL : LOOK = 53 BEFORE = 51 YOU = 61 LEAP = 34 OUTPUT : LEAP BEFORE LOOK YOU SYNTAX import java.io.*; class Potential {     public static void main(String args[])throws IOException     {         BufferedReader br=new BufferedReader(new Inp

SMITH NUMBER

PROGRAM: SMITH NUMBER A   Smith number  is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1). The first few such numbers are 4, 22, 58, 85, 94, 121 ……………….. For Example 58=> 5+8=13 2+(2+9) of 29 =13 Since sum of digits=sum of digits of prime factors 58 is a SMITH NUMBER SYNTAX: import java.io.*; class smith_no { static int sum,sum1;     public static void main(String args[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n;int c=0;         System.out.println("Enter a Number");         n=Integer.parseInt(br.readLine());         int n1=n;         while(n1!=0){         int d=n1%10;         sum1+=d;         n1=n1/10;}         for(int i=2;i<n;i++)         {             if(n%i==0)             {                 for(int j=2;j<i;j++)                 {                     if(i

MAGICAL/ MAGIC NUMBER USING RECURSION

PROGRAM :  MAGICAL NUMBER BY RECURSION This  program  checks if a  number  is a  Magic number  in  JAVA . A  number  is said to be a  Magic number  if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits. If the single digit comes to be 1 then the  number  is a  magic number . Example: 10999 => 1+0+9+9+9=28=>2+8=>10=>1+0= 1                  199    =>1+9+9 =>19=> 1+9=>10=>1+0= 1 SYNTAX import java.io.*; class magic  {    static int c;     public static void main(String arsg[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n;int d=0;int m;         System.out.println("ENter a NUmber");         n=Integer.parseInt(br.readLine());         m=n;         while(n!=0)         {d=n%10;             n=n/10;             c++;         }         magical(m);              }     public static void magical(int x)