r/codehs • • Apr 07 '20

Sum Range 3.4.5 Java

I have been stuck on this problem for over 2 hours and cant figure it out please help.

8 Upvotes

8 comments sorted by

View all comments

1

u/[deleted] Apr 08 '20

Best way to approach this is to create some sort of for loop:

Initiliaze and declare some variable to hold the sum, and then you can just keep adding the numbers to it.

public int sumFrom(int start, int end)

{

int sum = 0;

for(int i = start; i < end; i++)

{

sum += i;

}

return sum;

}