Monday, March 26, 2012

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

Have started solving various C# problems and am posting the solutions which run on my machine out here on this blog. Please feel free to comment on any errors, mistakes you spot and also in giving any kind of constructive feedback( negative or positive both welcome). (Using Visual Studio .NET 2008 with C#.) Below is the first problem from project euler.

Sum of all the multiples of 3 or 5 below 1000

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int sum=0;

for (int i= 0; i < 1000; i++)
{
if (i % 3 == 0 | i % 5 == 0)
{
sum = sum + i;
}
}
Console.WriteLine("Sum of all natural numbers multiple of 3 or 5 below 1000 is " + sum);
}
}
}

No comments: