+ 3
I want to find whether a number is palindrome or not if not i have to find the very next number which is palindrome please help
#include <iostream> using namespace std; int palind(int a){ int b=0; while(a>0){ b=b*10+a%10; a/=10;} if(a==b) return a; else return palind(a+1); } int main() { int s; cin>>s; cout<<palind(s); return 0; } this code isn't working please help me fixing it what's wrong with the above code?
1 Answer
+ 7
Maybe this?
#include <iostream>
using namespace std;
bool isPal(unsigned n) {
unsigned dig, rev=0, _num;
_num=n;
while(n!=0) {
dig=n%10;
n/=10;
rev=rev*10+dig;
}
if(rev==_num)
return true;
else
return false;
}
int main() {
unsigned num;
cin>>num;
if(isPal(num))
cout<<"Your number is a palindrome";
else {
cout<<"Your number is not a palindrome"<<endl;
for(unsigned i=num+1; i<=2*num; i++) {
if(isPal(i)) {
cout<<"Next number that is a palindrome is "<<i;
break;
}
}
}
return 0;
}