How to check if a number is prime cyclops in C++

1 Answer

0 votes
// A number with odd number of digits and zero in the center is cyclops number

#include <iostream>
#include <cmath> 

bool isCyclopsNumber(int n) {
    if (n == 0) {
        return true;
    }
     
    int m = n % 10;
    int count = 0;
    while (m != 0) {
        count++;
        n /= 10;
        m = n % 10;
    }
     
    n /= 10;
    m = n % 10;
    while (m != 0) {
        count--;
        n /= 10;
        m = n % 10;
    }
     
    return n == 0 && count == 0;
}
bool isPrime(int n) {
    if (n < 2) return false;
    
    double total = std::sqrt(n);
    for (int i = 2; i <= total; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
int main(void) {
 
    int n = 209;
    std::cout << (isCyclopsNumber(n) && isPrime(n) ? "yes" : "no") << "\n";
    
    n = 503;
    std::cout << (isCyclopsNumber(n) && isPrime(n) ? "yes" : "no") << "\n";

}
 
 
 
 
/*
run:
 
no
yes
 
*/

 



answered Mar 29, 2023 by avibootz

Related questions

1 answer 95 views
1 answer 98 views
1 answer 103 views
1 answer 124 views
1 answer 123 views
1 answer 103 views
1 answer 94 views
...