+ 2
Cpp
Pls may i know why I don’t get the output #include <iostream> using namespace std; int main() { int n,n1,n2=n,rev=0; cout<<"Enter any number"; cin>>n; while(n) { n1=n%10; rev=rev*10+n1; n=n%10; } if(n2==rev) { cout<<"num is palindrome";} else { cout<<"num is not palindrome";} return 0; }
2 Réponses
+ 1
1. Change n=n%10 to n=n/10.
Otherwise, the value of n never changes and the code is stuck in an infinite loop.
2. Declare n2=n after cin>>n;
Otherwise, an error will be thrown for the n2 variable since n hasn't been initialised yet.
https://code.sololearn.com/caiJ316bTBL4/?ref=app
0
error in code, while loop runs infinitely if the number n does not end at 0