#include #include #include char* rev(char buf[]) { for (int i = 0; i < strlen(buf); i++) { char t = buf[i]; buf[i] = buf[strlen(buf) - i - 1]; buf[strlen(buf) - i - 1] = t; } return buf; } char* convertToBase(int number, int base, char buf[]) { int idx = 0; char c[2]; while (number > 0) { sprintf(c, "%d", number % base); // if we were converting the base of anything bigger than // base10, we would have needed a special function to return // A - F for hexadecimal formats. buf[idx++] = c[0]; number /= base; } buf[idx] = '\0'; // we'll have to reverse the string before returning it, // because we're dividing the original number from the "one's" place -- remainder of // number % base is always the least significant bits return rev(buf); } int isPalindrome(char* str) { int ptra = 0; int ptrb = strlen(str) - 1; while (ptra <= ptrb) { if (str[ptra] != str[ptrb]) { return 0; } ptra++; ptrb--; } return 1; } int main() { int S = 25; int N = 3; int len = N; int* palindromeNums = (int*)malloc(len * sizeof(int)); int idx = 0; char buf[100]; while (N >= 0) { int cnt = 0; for (int i = 2; i < 11; i++) { char* retStr = convertToBase(S, i, buf); if (isPalindrome(retStr)) { cnt++; } if (cnt > 1) { palindromeNums[idx++] = S; break; } } S++; N--; } for (int i = 0; i < len; i++) { printf("%d\n", palindromeNums[i]); } }