Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C# multiples problem

  1. Dec 8, 2011 #1
    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 (Text):
    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
     
  2. jcsd
  3. Dec 8, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi rollcast! :smile:


    What is your purpose for the use of "continue" in your program?
     
  4. Dec 8, 2011 #3
    I thought I needed them to continue the loop after I performed the += operator?
     
  5. Dec 8, 2011 #4

    I like Serena

    User Avatar
    Homework Helper

    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.
     
  6. Dec 8, 2011 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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/
     
  7. Dec 8, 2011 #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?
     
  8. Dec 8, 2011 #7

    I like Serena

    User Avatar
    Homework Helper

    You interpreted the problem correctly.
    However, as it is now, you *always* continue, whether the number is a multiple of 3 or not.
     
  9. Dec 8, 2011 #8
    Code (Text):

    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?
     
  10. Dec 8, 2011 #9

    I like Serena

    User Avatar
    Homework Helper

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C# multiples problem
  1. Problems in C++ (Replies: 5)

  2. A problem with C (Replies: 4)

Loading...