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