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

  • Context: C# 
  • Thread starter Thread starter rollcast
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
8 replies · 6K views
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
 
Physics 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?
 
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?