Palindrome numbers | Sololearn: Learn to code for FREE!

+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; }

c++

4/4/2021 9:22:53 AM

Ahmed Azami Hassani

5 Answers

New Answer

+4

Instead of y=x%10 it should be y=m%10 and your code will work.

+1

Martin Taylor we love math 😂😂 Thanks bro

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; }