Can someone please explain this simple code?

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
  int m=12345;
  while(m>0)
{
	d=m%10;
	cout<<"d="<<d;
	m/=10;
	cout<<"m="<<m;
	n=10*n+d;
	cout<<"n="<<d;
}

        cout<<n<<endl;


So my output here will be 54321, but I really don't understand what's happening inside the loop. For the first run I get my d=5(12345%10),m=5(???), n=5(10*0+5).

I think I have explained what's my problem. How do I get m=5??
Last edited on
12345/10 is 1234 with a remainder of 5. Ergo, 12345%10 = 5. You then set m equal to 12345/10(int), which is 1234. Repeat.
I never got m=5. Using the assumption that n = 0 to start.

 d= 5 m= 1234 n= 5
 d= 4 m= 123 n= 4
 d= 3 m= 12 n= 3
 d= 2 m= 1 n= 2
 d= 1 m= 0 n= 1
54321
Press any key to continue . . .
Sorry it was my mistake. M is never 5. :(
Topic archived. No new replies allowed.