+ 1

Palindrome numbers

Why this code does not work #include <iostream> using namespace std; bool isPalindrome(int x) { int m=x, y, z=0; while (m!=0) { y=x%10; z=(z*10)+y; m=m/10; } if (x==z) 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; }

4th Apr 2021, 9:22 AM
Ahmed Azami Hassani
Ahmed Azami Hassani - avatar
5 odpowiedzi
+ 4
Instead of y=x%10 it should be y=m%10 and your code will work.
4th Apr 2021, 9:28 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
Martin Taylor we love math 😂😂 Thanks bro
4th Apr 2021, 1:41 PM
Ahmed Azami Hassani
Ahmed Azami Hassani - avatar
0
#include <iostream> using namespace std; bool isPalindrome(int reverse, int check) { bool status = true; if (reverse != check) { status = false; } return status; } int main() { int n, reverse = 0, rem; cin >> n; int check = n; while(n != 0){ rem = n % 10; reverse = reverse * 10 + rem; n /= 10; } if(isPalindrome(reverse, check) == 1) { cout << reverse <<" is a palindrome"; } else { cout << check <<" is NOT a palindrome"; } return 0; }
24th Sep 2021, 2:29 PM
Ben
Ben - avatar