forked from krishnamanojpvr/DSCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_Circular_Prime.cpp
50 lines (45 loc) · 1.09 KB
/
04_Circular_Prime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its digits will be prime. For example, 1193 is a circular prime, since 1931, 9311 and 3119 are also prime.
#include<iostream>
#include<cmath>
using namespace std;
bool prime(int num);
int main()
{
int n;
cin >> n;
int temp = n;
string number = to_string(n);
int lengthN = number.size();
int rem = 0;
bool flag = true;
int i = 0;
while(i<lengthN){
// int sum = 0;
cout << temp << "\n";
if(prime(temp) != true){
cout << n << " is not a circular prime";
flag = false;
break;
}
rem = temp%10;
temp = rem*pow(10,lengthN-1) + temp/10;
i++;
}
if(flag != false){
cout << n << " is a circular prime";
}
return 0 ;
}
bool prime(int num){
if (num>=2){
for(int i = 2; i < num; i++){
if(num%i == 0){
return false;
}
}
}
else{
return false;
}
return true;
}