0

Why is the output of my code for checking palindrome wrong for some of the tests in the practice question?

This is the code: #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int n, rev = 0, digit; do { digit = n % 10; rev = (rev*10) + digit; n/= 10; } while (n != 0); if (n == rev) 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; } https://code.sololearn.com/c4FAzrOSEzk7/?ref=app

26th Apr 2022, 9:32 AM
Ademola Uthman
18 Antworten
+ 3
I read bool isPalindrome(int x) { ... } And inside that function you never use that 'x'.
26th Apr 2022, 4:43 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 4
Where do you ever use the parameter 'x' in your function?
26th Apr 2022, 12:00 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
Ademola Akinsemoyin Uthman I would prefer to use arrays (work with all programming languages.) There are two possibilities; The string is odd in length (say 5), Check indexes (0,4) (1,3) and (2,2). If they are the same, strings are equal else NO. eg) MALAYALAM is a palindrome.
28th Apr 2022, 12:16 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Create a new cpp project in the playground, copy and paste your code and try some values and you will see the results.
26th Apr 2022, 9:43 AM
Stefanoo
Stefanoo - avatar
+ 1
Include in your question a link to your code in Code Playground
26th Apr 2022, 1:40 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado I've done that.
26th Apr 2022, 3:58 PM
Ademola Uthman
+ 1
I think Ani Jona 🕊 means that you inside your function you never used x. So that your given value(n) will never be used. If you print n inside your loop you will see what happens.
26th Apr 2022, 4:14 PM
Stefanoo
Stefanoo - avatar
26th Apr 2022, 4:44 PM
Stefanoo
Stefanoo - avatar
0
My code is running correctly in the playground but not in the code challenge.
26th Apr 2022, 10:23 AM
Ademola Uthman
0
Did you tested some inputs for both results? Like 123321 12321 123123 I tested your code and everything results with a "is a palindrome" 😉
26th Apr 2022, 10:28 AM
Stefanoo
Stefanoo - avatar
0
Okay, I edited the code and it's running for some numbers and not running for some. #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int rev = 0, digit, n; // num = n; do { digit = n % 10; rev = (rev*10) + digit; n/= 10; } while (n != 0); return n == rev; } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; } A number like 8888 will have a reserve of 888, which obviously results in 'NOT a palindrome' What did I get it wrong please?
26th Apr 2022, 10:37 AM
Ademola Uthman
0
Ani Jona 🕊 It's been replaced by 'int n' (actual parameter) when I called the isPalindrome () function.
26th Apr 2022, 4:03 PM
Ademola Uthman
0
I replaced x with n. That's why I have isPalindrome(n) in the main function.
26th Apr 2022, 4:17 PM
Ademola Uthman
0
Wow! Thanks a lot. It worked!
26th Apr 2022, 4:58 PM
Ademola Uthman
0
n value should be strored some where before comparing with rev
28th Apr 2022, 8:26 AM
Satish Kumar Janapureddi
Satish Kumar Janapureddi - avatar
0
An alternative solution: Convert the int to a string. Then, compare every string character to the "mirrored" one - first to last, second to second last, and so on. Go until you either find two different chars or reach the middle of the string. Return a boolean depending on which exit condition you found. Seemed fun for me.
28th Apr 2022, 11:02 AM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado Can a make a loop to automatically compare the characters without having to do that manually? So I can just input any word and the code runs automatically. Can you share a link to your code pls?
28th Apr 2022, 6:43 PM
Ademola Uthman
0
I don't understand what you mean by "automatically" and "manually" in the loop. Could you pls elaborate? My code: https://code.sololearn.com/cPdk1bUrEsGN
29th Apr 2022, 11:28 PM
Emerson Prado
Emerson Prado - avatar