0
I have tried with this code to no avail, does anyone know where the error lies
#include <iostream> using namespace std; bool isPalindrome(int x) { cout<<x;//👈how to fix this return isPalindrome; } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }
2 Réponses
+ 1
There is nothing to fix here. It is just that you dont have the code of the function yet to determine if a number is palindrome or not. Good Luck.
+ 1
not sure if this the best solution but it works!
#include <iostream>
#include <cmath>
bool isPolindrome(unsigned int x)
{
//step 1: how many digits in your number ex : 323 ---> d = 3
int d = 0, i = 0;
for (; i < 10; i++, d++)
if (pow(10, i) > x)
break;
//step 2: array of digits
int digits[d], a = pow(10, d-1);
for (i=d-1; i>=0; i--, a/= 10)
{
digits[i] = x / a;
x -= digits[i] *a;
}
/* uncomment this to see the array of digits
for (i=d-1; i>=0; i--)
std::cout<<digits[i]<<" ";
*/
//step 3: final step
int j=d-1;
for(i=0; i<j; i++, j--)
if (digits[i] != digits[j])
return false;
return true;
}