+ 1
Why it always say no even no. Is palindrome?
#include<iostream> using namespace std; bool isPalindrome(int num){ int reverse=0; int b; while(num!=0){ b=num%10; reverse=reverse*10+b; num=num/10; } if(num==reverse) return true; else return false; } int main(){ int n; cin>>n; if(isPalindrome(n)) cout<<"yes"; else cout<<"no"; }
5 odpowiedzi
+ 1
in function original num becomes 0 after loop so you are comparing
if ( 0 == reverse) so it false expect for 0 input..
Copy original num value, use copy in loop..
+ 1
Why it become zero after loop
+ 1
You are using num inside loop.
And modifying it's value by num=num/10 . Stop looping when num==0
Means when num!=0 loop continues otherwise stops.. Is not it?
+ 1
Thanks
+ 1
You're welcome..