# Code help! (adding multiple numbers together)

### Question

• I am a beginner trying to learn to code. I want to add multiple numbers together on a Console Application. My code as of following...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double number1;
double number2;
double total;

for (double numberInfi = 0; addmore == "n"; numberInfi++)
{
Console.WriteLine("enter more number? (Y/N)");

}

total = number1 + number2 + numberInfi;
}
}
}

I basically want number1 + number2 + number3 + etc until the user decided to stop adding more numbers/digits and sum its all up. I think the solution is on the for loop I am using but still not figure it out yet...

I know you guys may suggest different route to get this done but I want stick with my route of code so I can learn better logic handling.

Thank you!

Saturday, September 08, 2012 1:00 AM

• Hi,

here is how you should get started:

```            string text;
double num, total = 0D;
string message = "To do: type one number at a time.\r\n" +
"When you wanna sum them all up, just type \"exit\", ok?\r\n" +
"Ok, lets get started...";
Console.WriteLine(message);
while (true)
{
if (text.ToLower() == "exit")
break;
if (double.TryParse(text, out num))
total += num;
else
Console.WriteLine("NOTE: " + text + " is not number (will not be summed up, continue...");
}
Console.WriteLine("The total is " + total);

As you can see I use a while loop in the code, this means the code will circle to infinity, uless there is some condition that can break out of it. In my case I use to break, when user decides to sum all numers up (actually only to show them). To exit the loop I decided to use some simple example, by typing "exit" instead of a number - and in this case code leave while loop!

Check it out.

bye

Mitja

• Marked as answer by Sunday, September 16, 2012 7:06 PM
Saturday, September 08, 2012 8:03 PM

### All replies

• You can't just do the sum at the end, since you don't know how many values there will be.  Instead, you need to add to total earlier on.  Inside of your loop, just keep adding the new number to the existing total value.

If you set total to number1+number2 before the loop, every time the user adds another number, you can add to the total...

Reed Copsey, Jr. - http://reedcopsey.com

Saturday, September 08, 2012 1:30 AM
• Could you please fix my code...?
Saturday, September 08, 2012 7:00 PM
• Hi,

here is how you should get started:

```            string text;
double num, total = 0D;
string message = "To do: type one number at a time.\r\n" +
"When you wanna sum them all up, just type \"exit\", ok?\r\n" +
"Ok, lets get started...";
Console.WriteLine(message);
while (true)
{
if (text.ToLower() == "exit")
break;
if (double.TryParse(text, out num))
total += num;
else
Console.WriteLine("NOTE: " + text + " is not number (will not be summed up, continue...");
}
Console.WriteLine("The total is " + total);

As you can see I use a while loop in the code, this means the code will circle to infinity, uless there is some condition that can break out of it. In my case I use to break, when user decides to sum all numers up (actually only to show them). To exit the loop I decided to use some simple example, by typing "exit" instead of a number - and in this case code leave while loop!

Check it out.

bye

Mitja

• Marked as answer by Sunday, September 16, 2012 7:06 PM
Saturday, September 08, 2012 8:03 PM
• That looks pretty advance to me. I will try to dissect it a bit...
Saturday, September 08, 2012 8:29 PM