+ 6

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

8th Apr 2021, 3:47 AM
Utkarsh Tripathi
Utkarsh Tripathi - avatar
7 Answers
+ 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
8th Apr 2021, 9:34 AM
prashant kumhar
prashant kumhar - avatar
+ 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
8th Apr 2021, 7:14 AM
prashant kumhar
prashant kumhar - avatar
+ 2
Change 'x=n' to 'n=x' After while loop add: if(s==n) return 1; else return 0;
8th Apr 2021, 9:29 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 2
Remove n from bool function only take x and return true and false
8th Apr 2021, 9:32 AM
prashant kumhar
prashant kumhar - avatar
+ 1
Not working I have tried you answer
8th Apr 2021, 7:49 AM
Utkarsh Tripathi
Utkarsh Tripathi - avatar
+ 1
#include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int temp, rev=0, digit; temp = x; do { digit = temp % 10; rev = (rev*10) + digit; temp = temp / 10; } while (temp !=0); if (x == rev) 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; }
3rd Jan 2022, 9:38 PM
Muzaffar Najib
Muzaffar Najib - avatar
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; }
24th Sep 2021, 2:29 PM
Ben
Ben - avatar