-3

Stuck on this problem:

Given an integer num, return the sum of the multiples of num between 1 and 100. For example, if num is 20, the returned value should be the sum of 20, 40, 60, 80, and 100, which is 300. If num is not positive, return 0.

the code I already have (I'm not sure if it's at all right) is:

public int sumMultiples(int num)
{
    int sum = 0;

    for (int val = num; val <= 100; val++)
        sum = val;
    return sum;

}
5
  • 4
    You need to understand what each statement in your code does. Go through your for loop and see what does sum = val do to start with.
    – SomeDude
    Commented Oct 10, 2018 at 14:24
  • 2
    And what is your question? Did you run your code?
    – LuCio
    Commented Oct 10, 2018 at 14:26
  • all of these are getting me close (.75/1) but the missing part is what to return if the value is < 0. I know I should use an if statement but I'm not sure where to put it, should it be nested? should I put it at the end?
    – Kate
    Commented Oct 10, 2018 at 14:33
  • @kate as the first thing in the method, since doing the rest wouldn't make sense if the output is not correct Commented Oct 10, 2018 at 14:35
  • (I'm so sorry!) I'm new to stack overflow, why did my post get downvoted so many times?
    – Kate
    Commented Oct 10, 2018 at 14:46

8 Answers 8

2

First of all, you're not taking the sum but simply the last value.
Furthermore, you're increasing the value by 1 instead of the selected number.
In other words, try this:

public int sumMultiples(int num)
{
    if (num <= 0)
      return 0;

    int sum = 0;
    for (int val = num; val <= 100; val += num)
      sum += val;
    return sum;
}
0

an easy way to do that would be to ask if the counter can be divided by your number and if so, add the counter to the sum. So:

for(int i=0; i<=100; i++){
    if(i%num==0){
        sum = sum + i;
    }
}

Edit: of course you don't have to start your loop at 0, starting at num is better

3
  • Uch! If you do that, you'll look at (let's say we start with 20) 80 elements, and check if they're a multiple of 20. That's 80 comparaisons! It's more efficient to just check if your multiple of 20 is under 100, and break out when it's over this value ^^
    – Turtle
    Commented Oct 10, 2018 at 14:29
  • Sure it is, but it doesn't really matter if it takes 4 or 20ms to compute it and I wanted to keep it as easy as possible Commented Oct 10, 2018 at 14:31
  • Just my personal interpretation ofc, but I think it's harder to understand when you use a modulo operator.
    – Turtle
    Commented Oct 10, 2018 at 14:39
0

You are quite close. Your loop needs to increment by num so you get multiples, and you sum needs to be added to. You are overwriting sum every loop.

public int sumMultiples(int num)
{
    int sum = 0;

    for (int val = num; val <= 100; val += num)
        sum = sum + val;
    return sum;
}
0
public int sumMultiples(int num)
{
   if(num < 0)
       return 0;

   int sum = 0;
   for (int val = num; val <= 100; val++){
            if(val % num ==0 )
                 sum = sum + val;
   }
   return sum
}
0

Try this:

public int sumMultiples(int num)
{
    int sum = 0;

    for (int val = num; val <= 100; val += num)
        sum += val;

    return sum;
}
2
  • @FedericoklezCulloca Yeah, I realized when I tried to understand @Robert 's comment ^^ I checked the edits, saw he had a *=.
    – Turtle
    Commented Oct 10, 2018 at 14:34
  • @FedericoklezCulloca oh yeah, sure Commented Oct 10, 2018 at 14:37
0

Your code will always return 100, because you are putting into the sum variable su value val , which is increased by 1 each loop until reach 100.

It's also wrong increasing by 1 your val , because you have to increase it by your num.

You also forget to check if num < 0.

I suggest using a while here, more readable imho:

public int sumMultiples(int num){

    if(num < 0) return 0;

    int result = 0;
    int loop = num;
    while (loop <= 100){
        result += loop;
        loop += num;
    }

    return result;
}
0

This should work fine. The first if checks whether num is smaller than or equal to 0 (so not a positive number).

int sum = 0;
if (num <= 0) return 0;
for (int val = num; val <= 100; val+=num) {
    sum += val;
}
return sum;
1
  • 'Negative' and 'not positive' is not the same thing. Commented Oct 10, 2018 at 14:30
0

Number needs to be positive - for this you want an if statement saying that the number should be greater than 0. The code you have for sum of multiples needs to be changed. The code should look something like:

if(num >0) //you may want to add && num <=100
{
    int sum = 0;
    for (int val = num; val <= 100; val+=num)
    {
       sum += val; //for your example iteration 1 = 20, 2 = (20+40=60), 3 = (60+60 = 120), 4 = (120+80 = 200), 5 = (200+100=300)
    }
    return sum;
}
else
{
    Console.WriteLine("Number has to be greater than 0");
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.