To find the sum of all the multiples of 3 or 5 below or equal to 1000, first we have to find the sum of all the numbers which are divisible by 3, than sum of numbers which are divisible by 5. Once we are done with this we have to subtract the sum of numbers which are divisible by 3 and 5.
There are two approaches to solve this problem.

The first approach is to write a loop and find the numbers which are divisible by 3,5 and add them, after this subtract the numbers which are divisible by 3 and 5.

The other approach is to find the count of numbers which are divisible by 3,5 and 15. After this apply the arithmetic progressionâ€™s sum formula to find the sum.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int sum = 0; int range = 1000; int Number1 = 3; int Number2 = 5; for (int i = 0, j = 0; i <= range; i += Number1, j += Number2) { sum += i; if (j < range && j % Number2 == 0) { sum += j; } if (i <= range && i % Number2 * Number1 == 0) { sum = i; } } Console.WriteLine("\n"); Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000" + "using Loop " + sum); int Total_Number1 = range / Number1; int Total_Number2 = range / Number2; int Total_Number1AndNumber2 = range / (Number1 * Number2); int sumTotal_Number1 = Total_Number1 * (Number1 + Total_Number1 * Number1) / 2; int sumTotal_Number2 = Total_Number2 * (Number2 + Total_Number2 * Number2) / 2; int sumTotal_Number1andNumber2 = Total_Number1AndNumber2 * (Number2 * Number1 + Total_Number1AndNumber2 * Number2 * Number1) / 2; int total = sumTotal_Number1 + sumTotal_Number2  sumTotal_Number1andNumber2; Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000 " + "using arithmetic progressionâ€™s sum formula " + total); Console.ReadLine(); } } }
Please post your comments if you find a better way to find the sum of numbers which are divisible by 3 or 5 and less than 1000.
Filed under:
Number
Leave a comment