How to calculate consecutive integers using a for loop method and a recursive method?

American Idiot :

I need to calculate the sum of consecutive integers in 1.) a for loop method and 2.) a recursive method. Both methods are static int methods that take in two int parameters (one is the starting int and the other is the number of ints that come after it). For example, if I were to input (3, 3), the output should be 18 because 3 is the starting number and the 3 ints after are 4, 5, & 6. When you add those all up (3+4+5+6) you get 18. Both methods are doing this same math, except one is doing so with a for loop and the other is doing so recursively.

The problem I'm having here is my for loop method doesn't sum up properly. When I input (3, 3) the output is 31. Furthermore, I'm not quite sure how to write the recursive method since my for loop method doesn't work. May I get some help with this?

Also, no Arrays or ArrayLists. This code should be able to work without using those.

public static int consecSum(int startingNum, int numInts){
    for (int i = numInts; i>0; i--){
        startingNum += (startingNum + 1);
    }
    return startingNum;
}
public static int recursSum(int startingNum, int numInts) {
    if (startingNum == 0) {
        return 0;
    } else if (numInts == 0) {
        return startingNum;
    }
    return startingNum + recursSum(startingNum + numInts, numInts - 1);
}
3 \\startingNum (theres more code not shown where I use a scanner object to input these)
3 \\numInts
31 \\for loop output
\\The recursive method just prints an error message
ribbit :

For Loop

The problem in your for loop solution is what you think is the "last integer" is actually the "last sum". You meant to say

startingNum = 3
startingNum = 3 + 4
startingNum = 7 + 5
startingNum = 12 + 6

But since you're always keeping the new sum inside of startingNum itself, this is what's happening

startingNum = 3
startingNum = 3 + 4
startingNum = 7 + 8 (because startingNum + 1 = 7 + 1 = 8)
startingNum = 15 + 16

Try this instead

public static int consecSum(int startingNum, int numInts){
    int nextNum = startingNum + 1;
    for (int i = numInts; i>0; i--){
        startingNum += nextNum;
        nextNum++;
    }
    return startingNum;
}

Recursion

You almost have it. Based on what I see, your thought process was to have return the starting number if the number of integers is 0, otherwise return the starting number + the output of the method called on the next number. That's definitely valid. Try these edits

public static int recursSum(int startingNum, int numInts) {
    if (numInts == 0) {
        // eventually the numInts will become 0, meaning there's no
        // numbers to add after this startingNum, so just return it
        return startingNum;
    }

    // otherwise, if numInts > 0, that means there are other numbers to add so
    // return the sum of the current number with the output of the function called
    // on the next number (while not forgetting to decrease the number of integers
    // we should consider after that)
    return startingNum + recursSum(startingNum + 1 /* next number */, numInts - 1 /* decrease the number of integers to consider after that */);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=167103&siteId=1