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

  • C#
  • Thread starter rollcast
  • Start date
In summary, the conversation discusses finding the sum of all the multiples of 3 or 5 below 1000. The code provided by the individual is corrected to properly calculate the sum and avoid adding duplicates.
  • #1
rollcast
408
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
  • #2
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?
 
  • #3
I thought I needed them to continue the loop after I performed the += operator?
 
  • #4
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.
 
  • #5
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/
 
  • #6
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?
 
  • #7
You interpreted the problem correctly.
However, as it is now, you *always* continue, whether the number is a multiple of 3 or not.
 
  • #8
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?
 
  • #9
Yep! :smile:
 

What is "GORITHM" in C#?

"GORITHM" is a term used to refer to an algorithm that is used to solve a specific problem. In this case, the "GORITHM" is used to find the sum of multiples of 3 and 5 in C#.

Why is it important to find the sum of multiples of 3 and 5?

Finding the sum of multiples of 3 and 5 can be useful in many mathematical and programming applications. For example, it can help in solving mathematical puzzles, optimizing code performance, or in financial calculations.

How does the "GORITHM" work?

The "GORITHM" works by iterating through a range of numbers and checking if each number is a multiple of 3 or 5. If it is, the number is added to the total sum. This process is repeated until all numbers in the range have been checked.

What are the inputs and outputs of the "GORITHM"?

The inputs of the "GORITHM" are the starting and ending numbers of the range to be checked. The output is the sum of all multiples of 3 and 5 within that range.

Can the "GORITHM" be modified to find the sum of multiples of other numbers?

Yes, the "GORITHM" can be modified to find the sum of multiples of any given numbers. The key is to change the conditions for checking if a number is a multiple of a given number. For example, to find the sum of multiples of 2 and 7, the condition would be if a number is divisible by 2 or 7.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
1
Views
735
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
6
Views
884
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
Back
Top