C# GORITHM - Finding the Sum of Multiples of 3 and 5 in C#

  • Thread starter Thread starter rollcast
  • Start date Start date
AI Thread Summary
The discussion centers around solving a programming problem in C# that involves calculating the sum of all natural numbers below 1000 that are multiples of 3 or 5. An initial code snippet was shared, which incorrectly used the "continue" statement, leading to confusion about its purpose. Participants clarified that the "continue" statement was unnecessary and pointed out that it caused the loop to skip certain iterations. The correct approach was suggested, which simplifies the logic by using a single conditional statement to check for multiples of either 3 or 5. The revised code effectively calculates the desired sum without redundancy, demonstrating a clearer understanding of the problem and proper coding practices.
rollcast
Messages
403
Reaction score
0
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

I'm new to C# and I'm trying to solve this problem but there's no solution availible on Project Euler for it. I have made this bit of code and the result seems ok.

Code:
using System;

class MultipleSum
{
    public static void Main()
    {
        int sum = 0;

        for (int i = 0; i < 1000; i++)
        {
            if (i % 3 == 0)
                sum += i;
            continue;

            if (i % 5 == 0)
                sum += i;
            continue;
        }

        Console.WriteLine(sum);
        Console.WriteLine("Press ANY Key to continue");
        Console.ReadKey();
    }
}

Will that work for the problem?

Thanks
AL
 
Technology news on Phys.org
Hi rollcast! :smile:


rollcast said:
I'm new to C# and I'm trying to solve this problem but there's no solution availible on Project Euler for it. I have made this bit of code and the result seems ok.

Code:
using System;

class MultipleSum
{
    public static void Main()
    {
        int sum = 0;

        for (int i = 0; i < 1000; i++)
        {
            if (i % 3 == 0)
                sum += i;
            continue;

            if (i % 5 == 0)
                sum += i;
            continue;
        }

        Console.WriteLine(sum);
        Console.WriteLine("Press ANY Key to continue");
        Console.ReadKey();
    }
}

Will that work for the problem?

Thanks
AL

What is your purpose for the use of "continue" in your program?
 
I thought I needed them to continue the loop after I performed the += operator?
 
If you "continue" the loop, the rest of the loop is skipped and the next iteration starts.
Was that your intention?

Btw, the second "continue" is at the end of the loop and has as such no function.
 
rollcast said:
I thought I needed them to continue the loop after I performed the += operator?

Either you are using English in a fairly loose way, or you need to find out what the continue statement really does.

FWIW your code "works" because of the first continue statement, but the second one does nothing. But you wouldn't normally use a continue statement the way you used it.

http://www.cplusplus.com/doc/tutorial/control/
 
I was using the continue statements so in the event of a number, eg 60, that is both a multiple of 3 and 5 to stop it getting added twice? Or did I misinterpret the problem?
 
You interpreted the problem correctly.
However, as it is now, you *always* continue, whether the number is a multiple of 3 or not.
 
Code:
using System;

class MultipleSum
{
    public static void Main()
    {
        int sum = 0;

        for (int i = 3; i < 1000; i++)
        {
            if (i % 3 == 0 || i % 5 == 0)
            {
                sum += i;
            }

        }

        Console.WriteLine(sum.ToString());
        Console.WriteLine("Press ANY Key to continue");
        Console.ReadKey();
    }
}

Is this code more correct then?
 
Yep! :smile:
 

Similar threads

Replies
18
Views
2K
Replies
23
Views
2K
Replies
13
Views
5K
Replies
25
Views
2K
Replies
5
Views
3K
Replies
13
Views
2K
Replies
36
Views
278
Replies
10
Views
2K
Back
Top