A C++ begginer here
i was asked to write a prog to generate the reverse of a number using for loops
and here is the code
int num,rev=0,digit;
cout<<"enter no";
cin>>num;
for(int i=num;i>0;i=i/10)
{
digit=i%10;
rev=rev*10+digit;
cout<<rev;
}
/*
OUTPUT
enter no123
332321
*/
I ignored the headers and the main function
as i think the prob is in this part only.
Help would be appreciated.
You cout<<rew inside the for loop. On the first cycle rev is 3, on the second it is 32, on the third it is 321, so what you see is 332321. Move it outside and it will work fine.