+ 1

Why this code go wrong when i put s=0; in the initial conditions and give a true response when i put this condition after n=i;

#include <iostream> using namespace std; int main() { int a,s=0,i,n; for (i=11;i<=99;i++) { n=i; cout<<i<<":"; while (n!=0) { a=n%10; s=s*10+a; n=n/10; } cout<<s<<endl; } return 0; }

30th Aug 2020, 4:42 PM
Nariman Tajari
Nariman Tajari - avatar
3 Answers
+ 2
Nariman Tajari this change in behaviour is due to the value updated in 's' each time the value of 'n' changes. You must make the variable 's' equal to 0 each time after your while loop is completed. If you do not make s=0 inside the for loop then it will always print the updated value of 's' which will keep on increasing after each iteration.
30th Aug 2020, 5:39 PM
Avinesh
Avinesh - avatar
+ 4
What Output you expecting can u tell please
30th Aug 2020, 4:56 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
I expect the output like this for ex: 12:21 13: 31 14:41 .... But if i put s=0 in the place i mentioned i face with a bad output like 12:2113 ....
30th Aug 2020, 5:00 PM
Nariman Tajari
Nariman Tajari - avatar