2
Good morning, I need to write a code that receives a number and returns the sum of the digits of that number.
For example, if the user type 123456789 the program must print 45 (1+2+3+4...).
I think the easiest way would be to store the number in a string and then add the elements in each position of the string, but I don’t know how to add the elements of the string.
Some websites suggest using the function atoi()
but it takes the value of the whole string and not of a specific element.
In C vectors has no properties. So the
digitos.Length
should be replaced directly by 6. Or, in this case, there is the option to use the operatorsizeof
to derive the size of the vector (but only because the statement is in the block itself of the size calculation; if it had in a parameter passage, I do not guarantee that thesizeof
work properly)– Jefferson Quesado
Another thing, on the first
printf
:printf("%d (", total);
. With the%s
theprintf
will interpret the argument as a pointer tochar
null terminated, which is possibly not the desired– Jefferson Quesado