Leroy de Smet
Leroy de Smet

Reputation: 63

How do I sum up every 5th number instead of all numbers?

I need some help with the for-loop. I'm trying to sum up every fifth number that I type in, instead it sums them all up. What do I have to change?

        int count = 0;
        double total = 0;

        Console.Write("Enter your number: ");
        int input = int.Parse(Console.ReadLine());

        while (input != 0)
        {
            count++;
            for (count = 0; count <= 0; count += 5)
            {
                total = total + input;
            }
            Console.Write("Enter your number: ");
            input = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("The sum of every +5 numbers is: {0}", total);
        Console.ReadKey();

Upvotes: 3

Views: 935

Answers (5)

iSR5
iSR5

Reputation: 3498

Your sample would go forever, because there is no break point in your loop. You should always put a break point in your loop, otherwise it'll loop indefinitely.

Here is what you need :

int total = 0;
int count = 0;

Console.Write("Enter your number: ");

while (true)
{
    int input = 0;

    bool isNumber = int.TryParse(Console.ReadLine(), out input);

    if (isNumber)
    {
        count++;

        if (count % 5 == 0)
            total += input;
    }
    else
    {
        break;
    }

    Console.Write("Add another number or press enter to get the sum : ");
}

Console.WriteLine("The sum of every +5 numbers is: {0}", total);
Console.ReadKey();

So, you'll need first to put the user input inside a loop, and keep asking the user for adding another number until hits the condition where you close this loop. In the example, I decided to break the loop if the user typed anything not a number. but I told the user to press enter to get the some, to end the loop. For you, you'll need to translate that to your application breakpoint, how would you want the user to get the sum ?. Then, change the condition to your logic, so it breaks the loop and gets the sum.

another point is that int.TryParse. When you want to convert strings to numbers (int, long, decimal ..etc). You should always use `TryParse, this will verify the number, if the conversion failed, it'll return false. This way you can maintain the conversion and do something about it.

Upvotes: 0

ThePerplexedOne
ThePerplexedOne

Reputation: 2950

You could just store the numbers in a list and calculate it at the end:

var numbers = new List<int>();
Console.Write("Enter your number: ");
var input = int.Parse(Console.ReadLine());
while (input != 0)
{
    numbers.Add(input);
    input = int.Parse(Console.ReadLine());
}
var total = numbers.Where((x, i) => (i + 1) % 5 == 0).Sum(); // i + 1 since indexes are 0-based.
Console.WriteLine("The sum of every +5 numbers is: {0}", total);

Upvotes: 1

Ahmed Msaouri
Ahmed Msaouri

Reputation: 316

I think the best way is to recover all the values ​​before making the sum, this code works:

double total = 0; int input = -1; List<int> allInput = new List<int>(); while (input != 0) { Console.Write("Enter your number: "); input = int.Parse(Console.ReadLine()); allInput.Add(input); } for (int i = 0; i < allInput.Count()-1; i += 5) { total = total + allInput[i]; } Console.WriteLine("The sum of every +5 numbers is: {0}", total); Console.ReadKey();

Upvotes: 0

user12031933
user12031933

Reputation:

You can try this:

double total = 0;
int passover = 4;
int input = 0;
do
{
  passover++;
  Console.Write("Enter your number: ");
  int.TryParse(Console.ReadLine(), out input);
  if ( passover != 5 ) continue;
  passover = 1;
  total = total + input;
}
while ( input != 0 );
Console.WriteLine("The sum of every fifth numbers is: {0}", total);
Console.ReadKey();

Upvotes: 0

Martin
Martin

Reputation: 16423

Assuming that you enter a list of numbers, and the 1st number and every five afterwards is added (so 1st, 6th, 11th, etc.):

int count = 0;
double total = 0;

Console.Write("Enter your number: ");
int input = int.Parse(Console.ReadLine());

while (input != 0)
{
    count++;
    if (count % 5 == 1)
        total = total + input;
    Console.Write("Enter your number: ");
    input = int.Parse(Console.ReadLine());
}
Console.WriteLine("The sum of every +5 numbers is: {0}", total);
Console.ReadKey();

This works by using the modulo operator (%). The modulo operator returns the remainder of a division operation involving the number you specify.

In the code if (count % 5 == 1), the question is:

Is the remainder of count divided by 5 equal to 1?

If so, it adds the number. If not, it is skipped

The reason the remainder is one is because we want results 1, 6, 11, etc:

1 / 5 = remainder 1
6 / 5 = remainder 1
11 / 5 = remainder 1

If you change the modulo value to 0 it will return the results at position 5, 10, 15, etc.

Upvotes: 4

Related Questions