C++ Palindrome Numbers help | Sololearn: Learn to code for FREE!

+5

C++ Palindrome Numbers help

Can't solve this after many trials If i put this πŸ‘‡πŸ» only test case 2,5,6 and then 1,3,4 case is coming wrong trying from 3-4 days πŸ™πŸ»help #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; }

c++

4/8/2021 3:47:48 AM

Utkarsh Tripathi

8 Answers

New Answer

+1

I have yet to understand why people try to use convoluted math to determine if something is a palindrome. A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward. The problem is one of comparing characters. It does not matter if those characters represent numbers, alphabetical characters, punctuation or hieroglyphs. https://code.sololearn.com/cA2A16A11A16/#cpp

+6

#include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int y=x,z=0; while(x!=0) { z=z*10+x%10; x=x/10; } if(y==z) return(true); 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; } It work run it

+2

You didn't have to write x=n because n is not the same as in main function In function you remove n and replace it by x And also you have to return true and false in bool function

+2

Change 'x=n' to 'n=x' After while loop add: if(s==n) return 1; else return 0;

+2

Remove n from bool function only take x and return true and false

+1

Not working I have tried you answer

+1

Utkarsh Tripathi, who's answer is not working? I can assure you that my code works.

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