I am doing a coding exercise which is :
Have the function ThreeFiveMultiples(num) return the sum of all the multiples of 3 and 5 that are below num. For example: if num is 10, the multiples of 3 and 5 that are below 10 are 3, 5, 6, and 9, and adding them up you get 23, so your program should return 23. The integer being passed will be between 1 and 100.
I am not sure where the error in my c# solution is and i'd like to have help to solve this problem thank you.
I tried this in C# but get the error : System.IndexOutOfRangeException: Index was outside the bounds of the array
using System;
using System.Linq;
class MainClass {
public static int ThreeFiveMultiples(int num) {
//var total = 0;
int[] array = new int[] {};
for(var i = num-1; i>1; i--)
{
if(i%5 == 0 || i%3 == 0)
{
array[i] = i;
}
}
return array.Sum();
}
This is the solution in javascript
function ThreeFiveMultiples(num) {
var arr = [];
var tot=0;
for(var i=num-1; i>1; i--){
if(i%5===0 || i%3===0){
arr.push(i);
}
}
for(var i=0; i<arr.length; i++){
tot = tot+ arr[i];
}
return tot;
}