Palindrome numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 18

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

18th Dec 2020, 4:44 AM
John Christian Lopez
John Christian Lopez - avatar
22 Answers
+ 30
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; }
20th Mar 2021, 3:19 AM
Salvatore Iaquinto
Salvatore Iaquinto - avatar
+ 6
#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
7th Feb 2021, 9:06 AM
Anon
Anon - avatar
+ 5
John Christian Lopez Return true if previous value and new value is equal otherwise return false.
18th Dec 2020, 5:40 AM
A͢J
A͢J - avatar
+ 4
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; }
27th Feb 2021, 10:39 PM
Preshen Dookhi
Preshen Dookhi - avatar
+ 1
//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; }
17th Feb 2021, 6:23 PM
Jeednya Sade
Jeednya Sade - avatar
+ 1
#include <iostream> #include <string.h> #include <stdlib.h> #include <cmath> using namespace std; void verNumber(int arr[], int len, int num){ for (int i = 0; i < len ; i++) { cout <<i<<" "<<arr[i]<<endl; } if((arr[0] == arr[len-1]) && (arr[1] == arr[len-2])) { cout<<num<<" is a palindrome"<<endl; }else { cout<<num<<" is NOT a palindrome"<<endl; } } void isPalindome(int num, int len){ int arrNum[len]; for (int i = 0; i < len ; i++) { arrNum[i] = (int) floor(num/pow(10,(len-(i+1)))) % 10; } verNumber(arrNum,len,num); } int main(){ char data[5]; cin>>data; int len = strlen(data); int num = atoi(data); isPalindome(num,len); return 0; }
6th Jan 2022, 5:55 PM
cristian cortes
cristian cortes - avatar
+ 1
short and good! #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;}
6th Oct 2022, 11:15 AM
Marcel Krättli
+ 1
int n=84648 r, sum=0, temp; // for example n is = 84648 cout <<n; temp=n; // 84648 while(n>0) { //loop until n =0: first 2th 3th 4th 5th r=n%10; // 8 4 6 4 8 sum=(sum*10)+r; // 8 84 846 8464 84648 now is sum = 84648 n=n/10; // 8464,8 846,48 84,648 8,4648 0,84648 n is int therefore doesn't include after comer ,84648 }
24th May 2023, 12:09 PM
Mohammad Ommshi
Mohammad Ommshi - avatar
0
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?
18th Dec 2020, 6:50 AM
Javadev
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????
6th Jan 2021, 7:46 AM
MyOwnThoughts
MyOwnThoughts - avatar
0
Umm hi guys, How are you all
6th Jan 2021, 10:24 AM
Schawnnara Jiri Jeena-Myrr
Schawnnara Jiri Jeena-Myrr - avatar
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> :)
16th Apr 2021, 8:27 AM
Hussam
Hussam - avatar
0
Visit here for better & easy solution :- https://code.sololearn.com/c0a209a25a0A
31st Jul 2021, 1:07 PM
RAJAT AGRAWAL
RAJAT AGRAWAL - avatar
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
23rd Aug 2021, 6:28 PM
Swadhin Kumar
Swadhin Kumar - avatar
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; }
8th Sep 2021, 6:21 PM
Aimen GM9
Aimen GM9 - avatar
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; }
24th Sep 2021, 2:28 PM
Ben
Ben - avatar
0
14th Oct 2021, 8:30 PM
Abyot Tilahun
Abyot Tilahun - avatar
0
Why the reason we used so much 10 ?? All answers so similar.
21st Dec 2021, 4:11 PM
Lalala
Lalala - avatar
0
#include <iostream> using namespace std; #include <string> void rev(string num,int len,int save){ string cache; for (int x = 1; x < len+1; x++){ cache+=num[len-x]; } cout << save; if ( to_string(save) == cache ){ cout << " is a palindrome"; } else{ cout << " is NOT a palindrome"; } } int main(){ string num; cin >> num; rev(num,num.length(),stoi(num)); return 0; }
17th Oct 2022, 3:28 PM
Mikita Wagner
Mikita Wagner - avatar
22nd Jul 2023, 1:44 PM
Shizuo