19th Aug 2021, 2:26 PM
Samael Morningstar
16 odpowiedzi
+ 5
You should divide by 10 a temp variable that contains original value until > 0 and create a new one multiplying by 10 and adding its modulus. Finally, compare original with reversed result. Example not palindrome: original, temp, reversed 12345, 12345, 0 12345, 1234, 5 = 0*10+5 12345, 123, 54 = 5*10+4 12345, 12, 543 = 54*10+3 12345, 1, 5432 = 543*10+2 12345, 0, 54321 = 5432*10 +1 12345 != 54321 => not palindrome Example palindrome: original, temp, reversed 121, 121, 0 121, 12, 1 = 0*10+1 121, 1, 12 = 1*10+2 121, 0, 121 = 12*10+1 121 = 121 => palindrome bool isPalindrome(int x) { int rev = 0, tmp = x; while(tmp>0){ rev = rev*10+tmp%10; tmp/=10; } return rev == x; }
19th Aug 2021, 2:49 PM
David García Prados
+ 4
Samael why you do not save your code on SL Playground and provide the link with your question? For this reason this must be done for checking your code by a helper.
19th Aug 2021, 3:03 PM
JaScript
JaScript - avatar
+ 4
Check this code, Samael. bool isPalindrome(int x) { //complete the function int rev_num = 0, ox = x; while (x>0) { rev_num *= 10; rev_num += (x%10); x /= 10; } if (ox == rev_num) { return true; } else { return false; } }
19th Aug 2021, 3:22 PM
David García Prados
+ 4
Exactly, you are welcome. And "i" variable is not needed
19th Aug 2021, 3:27 PM
David García Prados
+ 2
David García Prados Can you kindly check it again.. I have edited it
19th Aug 2021, 2:54 PM
Samael Morningstar
+ 2
Samael, I edited my comment
19th Aug 2021, 3:00 PM
David García Prados
+ 2
David García Prados Um.. 😅 t-that's .. Thanks a lot.. Can you kindly do one more help?? Can you kindly show me where did I go wrong?? Your code is the best but I want to know where was the mistake??
19th Aug 2021, 3:04 PM
Samael Morningstar
+ 2
JaScript I'm sorry.. I have edited it
19th Aug 2021, 3:06 PM
Samael Morningstar
+ 2
David García Prados JaScript This time I have edited it again.. Hope it helps
19th Aug 2021, 3:15 PM
Samael Morningstar
+ 2
David García Prados JaScript Ah.. Finally!!!! I have found it!!! The imposter!!! Just joking, Actually, I divided that numer by 10 before getting the last number.. As a result, at a time, the number became 0 before I had one more step left.. or you can say, one more digit So I had moved the line at the end of the function.. And everything went fine.. 🙂 Thanks guys
19th Aug 2021, 3:26 PM
Samael Morningstar
+ 2
David García Prados Yeah, Didn't notice that. Thanks again.
19th Aug 2021, 3:28 PM
Samael Morningstar
+ 1
https://code.sololearn.com/chWi7hXjd8eG // Correct By Abhishek kumar. #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int rev_num = 0, ox = x; while (x>0) { int temp=0; temp=x%10; rev_num=rev_num*10+temp; x/=10; } if (ox == rev_num) { return true; } else { return false; } } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }
20th Aug 2021, 1:54 AM
Abhishek Kumar
Abhishek Kumar - avatar
20th Aug 2021, 2:46 AM
Samael Morningstar
0
SANA. NET- 160
20th Aug 2021, 12:02 PM
ياسر عاشور
ياسر عاشور - avatar
20th Aug 2021, 12:03 PM
ياسر عاشور
ياسر عاشور - avatar
20th Aug 2021, 12:58 PM
Samael Morningstar