fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. using namespace std;
  5.  
  6. // Function to check if a number is prime
  7. bool isPrime(int num) {
  8. if (num < 2) return false;
  9. for (int i = 2; i <= sqrt(num); i++) {
  10. if (num % i == 0) return false;
  11. }
  12. return true;
  13. }
  14.  
  15. // Function to generate a rotation of digits
  16. int rotateNumber(int num) {
  17. string s = to_string(num);
  18. // Move first digit to the end
  19. string rotated = s.substr(1) + s[0];
  20. return stoi(rotated);
  21. }
  22.  
  23. // Function to check if a number is circular prime
  24. bool isCircularPrime(int num) {
  25. int rotations = to_string(num).length();
  26. int current = num;
  27.  
  28. for (int i = 0; i < rotations; i++) {
  29. if (!isPrime(current)) return false;
  30. current = rotateNumber(current);
  31. }
  32. return true;
  33. }
  34.  
  35. int main() {
  36. int number;
  37. cout << "Enter a number: ";
  38. cin >> number;
  39.  
  40. if (isCircularPrime(number))
  41. cout << number << " is a Circular Prime." << endl;
  42. else
  43. cout << number << " is NOT a Circular Prime." << endl;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5292KB
stdin
265
stdout
Enter a number: 265 is NOT a Circular Prime.