hi! im a newbie and i want to learn more on C i have a problem and i have no idea on how to add a 5 digit number example: input is 12345 and the output of 12345 is 15. please help on this one.
hi! im a newbie and i want to learn more on C i have a problem and i have no idea on how to add a 5 digit number example: input is 12345 and the output of 12345 is 15. please help on this one.
Would it help you to know that subtracting '0' from '1' though '9' gives you the integer value of that character?
'1' - '0' = 1
'2' - '0' = 2
etc.
Perhaps the easiest way of doing this is to use
fgets(charArrayName, sizeof(charArrayName), stdin);
to get the number into a string format. Then just "walk" the charArrayName[] char by char, and add up the values as you go (into an integer variable). For your answer, remember to show it using %d, and not %c.
(Each char needs the value of '0' subtracted from it, to get it's integer equivalent, as Jonsca nicely noted, above. )
There are other ways: such as peeling off each digit one at a time, but they are not quite as simple.
If you aren't familiar with fgets() yet, check it out - it can be very useful in all kinds of programs.
First thing is that if your digit is integer form or not if is it in integer then its a very simple task just separate the digit like this
12345%10=5
12345/10=1234
follow this step recursively
if your digit is not in integer form then follow the step given by ADAK
Best Of luck