+ 1
Where did I go wrong??
Palindrome number: https://code.sololearn.com/cbi89vwijMdH/?ref=app
16 Réponses
+ 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;
}
+ 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.
+ 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;
}
}
+ 4
Exactly, you are welcome. And "i" variable is not needed
+ 2
David García Prados Can you kindly check it again.. I have edited it
+ 2
Samael, I edited my comment
+ 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??
+ 2
JaScript I'm sorry.. I have edited it
+ 2
David García Prados JaScript This time I have edited it again.. Hope it helps
+ 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
+ 2
David García Prados Yeah, Didn't notice that. Thanks again.
+ 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;
}
0
Abhishek Kumar Thanks
0
SANA. NET- 160
0
ياسر عاشور 😅???