Question #100877

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. Get a number as an input from the user and display all the circular prime number below that number. To solve this question, you are NOT allowed to use any concept that we have not studied in our course so far.

Expert's answer

#include "iostream"

#include <cmath>

using namespace std;


int main()

{

int value;

cout << "Input value >=10 : ";

cin >> value;

if (value < 10 )

{

cout << "Too small." << endl;

return 0;

}

int i;



for (i = 10; i < value; i++)

{

int temp = i;

int check = 0;

int times = 0;

for (int j = 2; j < temp / 2; j++)

{

if (temp%j == 0)

{

check = 1;

break;

}

}

if (check == 1);

else

{

int new_temp = temp;

do

{

times++;

new_temp = new_temp / 10;

} while (new_temp >= 10);

int perm = pow(10, times);

for (int k = 0; k < times; k++)

{

temp = (temp % perm)*10 + temp / perm;

for (int j = 2; j < temp / 2; j++)

{

if (temp%j == 0)

{

check = -1;

break;

}

}

if (check != -1) check++;

}

if (check == times)

{

cout << i << endl;


}

}

}

system("pause");

return 0;

}


LATEST TUTORIALS
APPROVED BY CLIENTS