0

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.

4
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by prvnkmr194
0

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.

0

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.

Edited by Adak: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.