+ 1

Why do not all test cases work in C++ Palindrome Numers

i worte this code and testet it the testcases 8888 and 12345 do work some of the others dont but i dont know why #include <iostream> using namespace std; #include <math.h> int numbers(int s){ if (s < 10){ return 1 ; } else return 1 + numbers(s/10); } bool isPalindrome(int x,int size) { //complete the function int halfsize = size/2 + size%2; // number 8888 = size 4// halfsize =2 int multi = pow(10,halfsize); int firsthalfnumber = x/multi ; // firsthalfnumber 8876 = 88 double dmulti = pow(10,halfsize); double doublefirstnumber = firsthalfnumber; double secondhalfnumber = (x/dmulti) - doublefirstnumber ; //0.76 secondhalfnumber = secondhalfnumber*(multi);//76 // That both are double firsthalfnumber must become a double if (secondhalfnumber - firsthalfnumber <0.2){ return true; }else return false; } int main() { int n; cin >>n; int size = numbers(n); // how many digits has the number = size if(isPalindrome(n,size)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }

20th Oct 2022, 6:52 PM
Erik Lachmann
Erik Lachmann - avatar
4 Answers
0
I don't understand your logic of last 5 statements of your function code.. What are you trying there actually? A polindrome number is a number which reverse is also result same number. Like 2332 , 23432 are polindrome numbers. 2345 , 1002 are not polindromes because those reverse are 5432, 2001 are different than original inputs... reverse(n) == n => polindrome else not
20th Oct 2022, 7:26 PM
Jayakrishna šŸ‡®šŸ‡³
+ 3
A palindrome numbers are ie 121, 1331 etc. Try to write with paper and pencil with own words how do you want to test it and test with these numbers if that works. After that you can write this algorithm in your code.
20th Oct 2022, 7:07 PM
JaScript
JaScript - avatar
+ 1
I found my mistake if the mumber is 7667 i compared 76 and 67 i had wrong thinking If i have a number 8876 to get the first half i divide it by 100 so my result is 88,76 because i have int the result is 88 For the second half i use double so my result is 88,76 now i subtract the first half from the second half and i get 0,76 now i multiply with 100 to get 76 now i am looking if 88 == 76 it its true it is a palindrome So in easy word i make two equal parts of my number and look if both are the same but i forgot to reverese the second half Ps: Should i delete this question or should i let it here if others have similar questions?
21st Oct 2022, 12:26 PM
Erik Lachmann
Erik Lachmann - avatar
+ 1
It's up to you.. Only dublicates are requested to delete Or removed. If it not dublicates then it may help others. If are able to reverse, second half then why not you reverse entire number so you don't need dividing , reversing,... unnecessary complicated things...!!! Simply * take number * reverse it. * compare
21st Oct 2022, 1:10 PM
Jayakrishna šŸ‡®šŸ‡³