Multiples of 3 and 5
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.
Find the sum of all the multiples of 3 or 5 below 1000.
Sinppet
public class Program
{public static void Main(string[] args){Console.WriteLine(Problem1.GetMultiplesOf3or5(1000));}}class Problem1{internal static long GetMultiplesOf3or5(long limit){long sum = 0;for (long i = 3; i< limit; i++){if ((i % 3 == 0) || (i % 5 == 0)){sum += i;}}return sum;}}
Steps
- Loop from 3 to 1000
- Check if number is multiple of 3 or 5
- If true, add it to the sum variable
No comments:
Post a Comment