Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ex. Enter 5 digits: 12345

1+2+3+4+5

please help me I'm still a students and wants to learn more from the experts, thanks :)
Posted
Comments
ZurdoDev 12-Dec-14 8:09am    
Where are you stuck? Are you doing this as a console app or as a windows forms app?
Jacob Cosare 12-Dec-14 8:25am    
this is just a console application... the teacher only taught us about ReadLine and ReadKey not about parses so we don't know what to do
Maciej Los 12-Dec-14 8:09am    
What have you tried? Where are you stuck?
[EDIT]
It's quite simple: you need to loop through the collection of numbers and add each other. That's all.
Jacob Cosare 12-Dec-14 8:27am    
How can I loop them if the numbers aren't consequent?
Pheonyx 12-Dec-14 8:11am    
well, the best way to learn is to try things and then ask rather than just ask.
So let's start with:
1) What type of application are you making?
2) What have you tried?
3) Have you got to the point where you are reading in characters?
4) Have you looked at String.Split on the MSDN site?
5) Have you looked at Int32.Parse or Int32.TryParse (again from the MSDN)

There are three parts to this:
1) Read the line of text - this is trivial, and I assume you know how to do that.
2) Add the digit values together
3) Print the result - again this is trivial.

The middle bit is a little complicated to think about, but it's really pretty easy:
1) Set up a "total" variable, and start it at zero.
2) Loop through each character in the input.
2.1) If the character isn't a digit, ignore it, or report a problem and stop.
2.2) Otherwise, convert it to an integer value. This sounds complicated, but it isn't:
C#
char c = '2';
int x = (int) (c - '0');
Will leave the value two in the variable x
2.3) Add the new value into the total
2.4) Repeat for next character at (2) until you run out of characters.

And the loop is realy easy:
C#
foreach (char c in myInputString)
   {
   ...
   }

So give it a try, and see how far you get. It's pretty simple now you have all the pieces.
 
Share this answer
 
Comments
Maciej Los 12-Dec-14 8:35am    
Short and to the point! +5!
Jacob Cosare 12-Dec-14 10:03am    
Thanks for the tips sir and I surely appreciate its usefulness in me :)
OriginalGriff 12-Dec-14 10:14am    
You're welcome!
Solution1 by OriginalGriff is excellent!

When you finish learning about basics, you'll be still interested of programming, i'd suggest to read about Linq[^]. It's very interesting language ;)

C#
string s = "12345";
int sumOfDigits = s.Select(c=>Convert.ToInt32(c.ToString())).Sum();
Console.WriteLine("Sum of digits: '{0}' is {1}", s, sumOfDigits);
//or 
//as OriginalGriff suggested
sumOfDigits = s.Select(c=>Convert.ToInt32(c-'0')).Sum();
Console.WriteLine("Sum of digits: '{0}' is {1}", s, sumOfDigits);
 
Share this answer
 
Comments
Jacob Cosare 12-Dec-14 10:03am    
Thank you so much for this code I'm going to study this code and I sure will enhance this as long as I live :)
Maciej Los 12-Dec-14 10:40am    
You're very welcome ;)

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