I have a problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a problem

Hello I need to find out how many palindrome numbers are in the range [a, b]. Where did I go wrong? #include<iostream> using namespace std; int main() {int a,b,i,i1,ogl=0,P; cin>>a>>b; for(i=a;i<=b;i++) {i1=i; while(i1!=0) {ogl=ogl*10+i1/10; i1=i1/10; } if(ogl==i) P++; } cout<<P; return 0; }

28th Nov 2020, 10:14 AM
HAPYX 1
HAPYX 1 - avatar
2 Answers
+ 2
You need to initialize 'P' to a proper value before performing calculations on it, in this case you will want to give it an initial value of zero. You also need to reset the value of "ogl" inside the loop, otherwise it is going to accumulate the values of all the previous iterations.
28th Nov 2020, 10:40 AM
Shadow
Shadow - avatar
0
thanks a lot!
28th Nov 2020, 10:41 AM
HAPYX 1
HAPYX 1 - avatar