Palindrome numbers | Sololearn: Learn to code for FREE!

-11

Palindrome numbers

#include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }

c++

12/18/2020 4:44:34 AM

John Christian Lopez

16 Answers

New Answer

+11

My answer plus an explanation on how this works since that's what's important: #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int num, rem, rev=0; num=x; //to be able to save new values to compare original with while(x != 0)//to end loop once x=0 { rem = x%10; //1st step divides number by 10 and makes the remainder the value of rem. Ex. 1234/10=123.4 rem=4 rev= rev*10 + rem; //reverse number starts at 0 *10 + rem(4)=4 x /= 10; //x becomes the value of x/10(123) //these steps repeat. Rem=3, rev =4*10+3=43, x=123/10=12, until rev= 4321 and x=0. } if (rev==num) { 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; }

+5

John Christian Lopez Return true if previous value and new value is equal otherwise return false.

+3

#include <iostream> using namespace std; int main() { int n,r,sum=0,temp; cin>>n; cout <<n; temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) cout<<" is a palindrome"; else cout<<" is NOT a palindrome"; return 0; } Thats the only thing you are looking for

+1

Umm hi guys, How are you all

+1

bool isPalindrome(int x) { //complete the function int n, rev = 0, rem; n = x; while (n > 0) { rem = n%10; rev = rev*10 + rem; n /= 10; } return rev == x; }

0

i am also on this problem and this answer that is provided does not work... i am glad it didnt work because i want to understand how it works and why they provided a global bool function. and then call the function in an if statement, ( i am guessing (n) is now == to x or the fucntion parameter.) im not sure what is going on here and i need help understnding what is going on so we can answer this problem correctly. ... why did they use a global bool function????

0

//with bool function #include <iostream> using namespace std; bool isPalindrome(int x, int y) { if(x==y){ return true ; } else{ return false ; } } int reverse(int z){ int r=0; while(z!=0){ r=r*10; r=r+z%10; z=z/10; } return r; } int main() { int n; cin >>n; int j =reverse(n); if(isPalindrome(n,j)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }

0

bool isPalindrome(int x) { int length = to_string(x).length() -1; string xxx; for (length; length != -1; length--){ xxx = xxx + to_string(x)[length] ; } if (xxx == to_string(x)) { return true; } else{ return false; } } you have to #include<string> :)

0

Visit here for better & easy solution :- https://code.sololearn.com/c0a209a25a0A

0

#include <iostream> using namespace std; int main() { int n,r,sum=0,temp; cin>>n; cout <<n; temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) cout<<" is a palindrome"; else cout<<" is NOT a palindrome"; return 0; } now it's working

0

use % module to reverse the whole number one by one and you could use a while loop to make it easy #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int neww, div, rem, rem1; // first part out of the loop rem1 = x % 10; // get the first number of the reversed x div = x / 10; // substract the last number of x neww = rem1; // begin the neww reverse number // reverse the whole number while ( div > 10 ) { rem = div % 10; neww = neww*10 + rem; div = div / 10; } neww = neww*10 + div; /* concatenate the first number of x as as the last one in the reverse*/ if ( x == neww) { 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; }

0

#include <iostream> using namespace std; bool isPalindrome(int reverse, int check) { //complete the function 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; }

0

-1

John Christian Lopez Your attempts?

-1

#include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int n,s=0,r; x=n; while(n!=0) { r=n%10; s=s*10+r; n=n/10; } } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }

-1

hmmm. looks like that code is wrong. maybe you pasted something else? hint looks like it takes in parameter x and overwrites it with n. also does not look like isPalindrome returns true or false (missing return statement). Additional, things to consider, maybe this could help you: — Are there other things that could be palindromes? other than numbers? are you going to rewrite this if you get asked to instead check if a string is a palindrome? would checking if a sting is a palindrome be easier than a number?