Web Development



Adding individual integers in C

By M4rc ·
I am somewhat new to C++ (in the sense that I have a pretty good understanding of the concepts of programming, I am just unable to write a substantial program consisting of more than a handful of lines of code...) and am unsure of the best way to accomplish what I want. What I want is to write a program where the user enters an integer and the program adds the individual digits and displays the results. Ex: user enters 215, program adds 2+1+5, and displays the results. I am using Visual Studio 2008 Express, if it is relevant. Any and all suggestions or help would be greatly appreciated. Thanks in advance!

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Well once you got the string of numbers

by Tony Hopkinson In reply to Adding individual integer ...

.net's string class will let you access it character by character, and teh Convert calls to strinr the char into a number so

So basically

int answer = 0;
for(index =0; index<input.Length; index++)
input+= Convert.ToInt(input[index]);

or some such would be one way.

Related Discussions

Related Forums