Omar Abdulghani
Omar Abdulghani

Reputation: 23

While loop, sum the fifth, tenth, fifteenth...etc number

Example of what needs to be done:

Example of what needs to be done

"Several numbers are entered (the input again ends with 0). Determine and print the sum of the 5th, 10th, 15th number, etc."

Can someone please tell me what's wrong with my code?

class Program
{
    static void Main(string[] args)
    {
        int counter = 0;
        int sum = 0;

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

        while (number != 0)
        {
            if (number > 0 && counter % 5 == 0)
            {
                counter++;
                sum = sum + number;
            }
            Console.Write("Enter a number: ");
            number = int.Parse(Console.ReadLine());
        }

        Console.WriteLine("Sum of the 5th, 10th, 15th ... number is = {0}", sum);
        Console.ReadKey();
    }
}

Upvotes: 2

Views: 711

Answers (2)

Mong Zhu
Mong Zhu

Reputation: 23732

Problem: you increment the counter only if % 5 == 0 but it never reaches that value. As you can see your starting value is:

int counter = 0;

So the second part of the if condition will evaluate to false in the first iteration and all subsequent iterations, because the variable never gets the chance to change. It remains at 0 in all iterations

Solution: put the incrementing line outside of the if-clause. This way you are actually counting the numbers of input numbers and thus you can identify whether it is the 5-th, 10-th, 15-th ...

Since you intend to count the input numbers I would suggest to put the

number = int.Parse(Console.ReadLine());

as the first line in the while loop. Then you can count up and after that you should evaluate wether this number is the 5-th, 10-th, 15-th ... element:

while (number != 0)
{
    Console.Write("Enter a number: ");
    number = int.Parse(Console.ReadLine());
    counter++;

    if (number > 0 && counter % 5 == 0)
    {
        sum = sum + number;
    }
}

EDIT: Inspired by the comment by PaulF :

I would assume that the counter should only be incremented if (number > 0).

To be correct in detail of your requirements you would need to make an extra if-clause to check for this case. But as 0 is the neutral element in an addition it does not make a difference to the calculated sum. So the code should still produce the desired outcome

EDIT 2:

If your intention was also to not allow negative numbers then you should split your if clause into two and increment the counter only if the entered number is higher than 0. Afterwards you can check whether the current element is a candidate to be added to the sum:

while (number != 0)
{
    Console.Write("Enter a number: ");
    number = int.Parse(Console.ReadLine());

    if (number > 0 )
    {
        counter++;
        if (counter % 5 == 0)
        {
            sum = sum + number;
        }
    }
    else
    {
        Console.Write("Please no negative numbers!");
    }
}

Upvotes: 8

Alesandro Giordano
Alesandro Giordano

Reputation: 381

You have to increment counter before if statment

Counter++
If (counter % 5 == 0) { 
...

Upvotes: 0

Related Questions