Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to check if a number is cyclops (number with odd number of digits and zero in the center) in C++

2 Answers

0 votes
#include <iostream>

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;
}

int main(void) {

    std::cout << (isCyclopsNumber(209) ? "yes" : "no") << "\n";
    std::cout << (isCyclopsNumber(18037) ? "yes" : "no") << "\n";
    std::cout << (isCyclopsNumber(5604) ? "yes" : "no") << "\n";
}




/*
run:

yes
yes
no

*/

 



answered Mar 28, 2023 by avibootz
0 votes
#include <iostream>
#include <sstream>

bool OnlyOneZero(std::string str) {
    int count = 0;
    
    for (char ch : str) {
        if (ch == '0') {
            count++;
        }
    }
    
    return count == 1;
}
 
bool isCyclopsNumber(int n) {
    if (n == 0) {
        return true;
    }
     
    std::stringstream stream;
    stream << n;
 
    std::string str;
    stream >> str;
 
    if (!(str.length() % 2)) {
        return false;
    }
 
    if (!OnlyOneZero(str)) {
        return false;
    }
 
    int mid_index = str.length() / 2;
    if (str[mid_index] == '0')
        return true;
 
    return false;
}
 
int main(void) {
 
    std::cout << (isCyclopsNumber(209) ? "yes" : "no") << "\n";
    std::cout << (isCyclopsNumber(18037) ? "yes" : "no") << "\n";
    std::cout << (isCyclopsNumber(5604) ? "yes" : "no") << "\n";
}
 
 
 
 
 
/*
run:
 
yes
yes
no
 
*/

 



answered Apr 11, 2023 by avibootz
...