### Eulers Problem - Double base palindrome

Hello guys, this program is supposed to work on Eulers Problem - double base palindromes. It is supposed to print both decimal and binary palindromes. I do not really know what the problem is within this program but in binary base, the checkStringPalindrome function is not working directly (the result is giving me non-palindromic binary numbers as well)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119`` ``````#include #include #include #include /* Main Function*/ int main() { bool checkNumPalindrome( long long int num ); bool checkStringPalindrome (char *s); void decToBinary( char *bin, int num, int *digits); long long int limit, num, sum; bool check1, check2; char bin[100] = {}; int digits = 0; printf("This Program checks for numbers within the limit you entered for Double" "base Palindromes in decimal base (10) and binary base (2).\n"); printf("\nPlease enter the limit: "); scanf("%lld", &limit); num = 1; sum = 0; while(num <= limit) { check1 = checkNumPalindrome(num); if( check1 == true) { decToBinary(bin, num, &digits); check2 = checkStringPalindrome(bin); if(check2 == true) { printf("\n%lld is a Double base palindrome.\n", num); printf("In Decimal (base 10) : %lld\n", num); printf("In Binary (base 2) : "); int i; for(i = 0; i < digits; i++) { printf("%d", bin[i]); } sum = sum + num; printf("\n"); } *bin = 0; } num++; } printf("\nThe total sum is %lld.\n", sum); return 0; } /*Function to check if a number is Palindrome*/ bool checkNumPalindrome( long long int num ) { long long int originalNum, reverseNum; int remainder; originalNum = num; reverseNum = 0; while( num > 0 ) { remainder = num % 10; reverseNum = reverseNum*10 + remainder; num/=10; } if(originalNum == reverseNum) return true; else return false; } /*Function to check if an array is a palindrome*/ bool checkStringPalindrome (char *bin) { int a,b; for (a = 0, b = strlen(bin)-1 ; a < b ; a++, b--) { if (bin[a] != bin[b]) return false; // Not palindrome } return true; //Palindrome } /*Function to convert a number from decimal base to binary using recursion*/ void decToBinary( char *bin, int num, int *digits) { int i, j, k; i = 0; int count = 0; while(num > 0) { bin[i] = num%2; num = num/2; i++; count++; } *digits = count; }``````

Last edited on
@cirise099
You will get a much better response if you put your code in code tags. Then people will be able to view the structure of your code and run it in c++ shell without having to download it and reformat it first.

You have an array bin[] and you are at times treating it as if it is char bin[] and at other times as int bin[]. You can do either ... but not both.

If you want bin[] to be an array of chars (as declared) then
`bin[i] = num%2;`
would have to become
`bin[i] = '0' + num%2;`
Otherwise every appearance of 0 in binary would actually put a null-terminator in, throwing out strlen.
Then you would have to null-terminate it:
`bin[count] = '\0';`
after which your parameter digits would become redundant.
You would also have to adjust the line
`printf("%d", bin[i]);`
since bin[i] is char not an int.

Alternatively, you could work with
int bin[]
instead. But then you would need to change bool checkStringPalindrome () to deal with an int array, and you would also have to pass it the number of digits, rather than use strlen.

So, basically, you will have to make up your mind whether you want char bin[] or int bin[]. Either can be made to work ... but you can't mix them.
Topic archived. No new replies allowed.