Taking a problem from the website, "Project Euler #1", I have created my first C# program. The problem I have used is as follows:
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.
The program I have made:
using System;
namespace _5_multiples
{
public class MainClass
{
public static void Main()
{
int currentNumber = 1000;
int allSum = 0;
currentNumber = currentNumber - 1;
while (currentNumber > 0)
{
if (currentNumber % 3 == 0)
{
allSum = allSum + currentNumber;
}
else
{
if (currentNumber % 5 == 0)
{
allSum = allSum + currentNumber;
}
}
currentNumber = currentNumber - 1;
}
Console.WriteLine(allSum);
}
}
}
I understand there is probably a lot to improve - this is my first C# program after all. I did run into a few problems such as referencing non static members from a static context and no suitable entry method. Rather than finding a definitive fix, the solution I implemented was more of a workaround; hence why I feel there may be a lot to improve.
namespace _5_multiples
- this doesn't look right? Name of the program is actually 3_5_multiples
- perhaps a badly chosen name?
Anyway, please let me know on anything that could be improved.
for
loop. \$\endgroup\$