Suppose there is a number 123. You need to add its numbers, that is, 1 + 2 + 3 (= 6).
Interested in using which function in stdio.h it can be done, and if not, then with what library?
Suppose there is a number 123. You need to add its numbers, that is, 1 + 2 + 3 (= 6).
Interested in using which function in stdio.h it can be done, and if not, then with what library?
I understand that the input is a number, not a string.
int n = 123; // число на входе int r = 0; while(n > 0){ int last = n % 10; // последняя цифра (% - остаток от деления) n = n /= 10 // уберем цифру r += last; }
In r we get the result.
There is an ancient trick - because all the digits go in a row in all encodings (? I have not met others in any case), then from the symbol you can get a digit from it '0', it is not particularly tied to the language (we were shown on Pascal). For exact implementation, you need to know what your string is stored in.
And stdio.h is pure si as far as I remember, definitely not visual c ++
Source: https://ru.stackoverflow.com/questions/255405/
All Articles