Why it always say no even no. Is palindrome? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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"; }

10th May 2022, 5:28 PM
Sonu Rajput
Sonu Rajput - avatar
5 Answers
+ 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..
10th May 2022, 5:32 PM
Jayakrishna 🇮🇳
+ 1
Why it become zero after loop
10th May 2022, 5:40 PM
Sonu Rajput
Sonu Rajput - avatar
+ 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?
10th May 2022, 5:45 PM
Jayakrishna 🇮🇳
+ 1
Thanks
10th May 2022, 5:53 PM
Sonu Rajput
Sonu Rajput - avatar
+ 1
You're welcome..
10th May 2022, 5:53 PM
Jayakrishna 🇮🇳