add digits of each double digit numner

140 views Asked by At

Here is my code. I want to have users enter any number of doubles continuously until 100 times (could be however number of times the user wants but less than 100). Display sum of all values entered. In my code, I don't know how to allow users to enter numbers continuously (im guessing you would have a while loop). Thanks a lot!

Console.WriteLine("Enter double");
    double.first = double.Parse(Console.ReadLine());

    while(first != 0)
    {
        Console.WriteLine("Enter double");
        int num = int.Parse(Console.ReadLine());
        double sum;
        while(num != 0)
            {
            double ten = num/10; 
            double tenth = Math.Floor(ten);
            double oneth = num % 10;
            sum = tenth + oneth; 
            Console.WriteLine("{0}", sum);
            break;

            }

        first = double.Parse(Console.ReadLine());

    }
2

There are 2 answers

0
Kevin On

Edited after comment:

Ok, I'm still assuming this is homework, but here are the basics...

I would do this in two phases, data input, then calcualtion.

Create something to store the input in and a counter for the loop

var inputs = new List<double>();
var counter = 0;

Now comes you while loop for your inputs...

while(counter < 100)
{
    var tempInput = 0.0D;
    Double.TryParse(Console.ReadLine(), out tempInput);
    if(tempInput == 0.0D)
    {
        // The user did not enter something that can be parsed into a double
        // If you'd like to use that as the signal that the user is finished entering data,
        // just do a break here to exit the loop early
        break;
    }
    inputs.Add(tempInput);
    // This is your limiter, once counter reaches 100 the loop will exit on its own
    counter++;
}

Now you can just perform the calcualtions on the values you've accumulated...

var total = 0.0D;
foreach(var value in inputs)
{
    total += value;
}

Now display the value in total.

Keep in mind there are a multitude of ways to do this this one is just an example to get you past the problem of aquiring the data.

0
transporter_room_3 On

I want to have users enter any number of doubles continuously until 100 times (could be however number of times the user wants but less than 100).

You need to keep track of 3 things.

  1. The next double.
  2. The running total.
  3. How many times the user has provided input.

Variables:

double next;
double runningTotal = 0;
int iterations = 0;

Now, to continuously receive input from the user, you can write a while-loop as you correctly identified. In this loop you should check for two things:

  1. That the next value is a double and that it is not 0.
  2. That the user has not provided input more than 100 times.

While-loop:

while (double.TryParse(Console.ReadLine(), out next) && next != 0 && iterations < 100) {
    // Count number of inputs.
    iterations++;

    // Add to the running total.
    runningTotal += next;
}

Display sum of all values entered.

Simply write to the console.

Output:

Console.WriteLine("You entered {0} number(s) giving a total value of {1}", iterations+1, runningTotal);

Complete example:

static void Main()
{
    double runningTotal = 0;
    double next;
    var iterations = 0;

    Console.Write("Enter double: ");
    while (double.TryParse(Console.ReadLine(), out next) && next != 0 && iterations < 100)
    {
        runningTotal += next;
        iterations++;
        Console.Write("Enter double: ");
    }
    Console.WriteLine("You entered {0} number(s) giving a total value of {1}", iterations+1, runningTotal);
    Console.Read();
}