How to get the first 100 cyclops numbers (number with odd number of digits and zero in the center) in C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>

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

void getFirstCyclopsUpTo(int total) {
    int n = 0, col = 0;

    while (total > 0) {
        if (isCyclopsNumber(n)) {
            printf("%7d", n);
            col++;
            if (col == 10) {
                printf("\n");
                col = 0;
            }
            total--;
        }
        n++;

    }
}

int main(void) {

    getFirstCyclopsUpTo(100);

    return 0;
}




/*
run:

      0    101    102    103    104    105    106    107    108    109
    201    202    203    204    205    206    207    208    209    301
    302    303    304    305    306    307    308    309    401    402
    403    404    405    406    407    408    409    501    502    503
    504    505    506    507    508    509    601    602    603    604
    605    606    607    608    609    701    702    703    704    705
    706    707    708    709    801    802    803    804    805    806
    807    808    809    901    902    903    904    905    906    907
    908    909  11011  11012  11013  11014  11015  11016  11017  11018
  11019  11021  11022  11023  11024  11025  11026  11027  11028  11029

*/

 



answered Mar 28, 2023 by avibootz
...