it's a program gets a number and adds it's digits,though i don't know
how the commented expressions does it!!!
Code:main() { int x, i, m=0; scanf( " %d " ,&x ); for( i=0 ; x>0 ; i++) { m += x % 10 ; /* */ x /= 10; /* */ } printf(" %d ", m ); }
it's a program gets a number and adds it's digits,though i don't know
how the commented expressions does it!!!
Code:main() { int x, i, m=0; scanf( " %d " ,&x ); for( i=0 ; x>0 ; i++) { m += x % 10 ; /* */ x /= 10; /* */ } printf(" %d ", m ); }
Line 6 and line 7, are using the fact that our number base is 10. So every time through the for loop, one digit from the right hand side is "peeled off", and separated from the rest of the number. m gives you a running sum of the digits.
When x == 0, the positive integer, has had it's digits all peeled off.
Don't be afraid to put a printf() statement inside the for loop, followed by a getchar(), so you can see what digit is being removed, and get a better feel for it. That's a big plug with programming - you don't have to just theorize - you can often times, quickly try it.
Also, not a big deal, but your for loop could easily be used as a while loop. Therefore no need for integer i.