Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need to create a program in c# that when inserted a string/integer it will add.

E.g:
Insert: 61 Output: 6+1=7
Insert: 234 Output: 2+3+4=9

I don't know how to code this, but I was thinking that the entered number should be stored in an array then it will called in a loop for them to add? I'm not really sure! Please help. Thanks!
Posted
Updated 12-Jul-14 23:48pm
v2

1 solution

Answer at - Sum of digits in C#[^].
C#
sum = 0;
while (n != 0) {
    sum += n % 10;
    n /= 10;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900