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

recursive method java
recursion using stack
recursive for loop java
recursive function example
understanding recursion java
recursion examples
two recursive calls in same function
base case recursion

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
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 */);
}

Recursive Programming, For example, the problem of adding (or multiplying) n consecutive integers can be a method sumR(n) (or timesR(n) ) that adds (or multiplies) integers from 1 to n, We can formally prove this statement by deriving a recursive equation for the � Write a Java Program to find GCD of Two Numbers using For Loop, While Loop, and recursive method. The Greatest Common Divisor (GCD) is also known as the Highest Common Factor (HCF), or Highest Common Divisor (HCD), or Greatest Common Factor (GCF), or Greatest Common Measure (GCM).

The problem with the iterative solution is that you're modifying the starting point in each iteration. Instead, could use a separate variable for the sum:

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

The recursive implementation has a similar problem:

public static int recursSum(int startingNum, int numInts) {
    if (numInts == 0) {
        return startingNum;
    }
    return startingNum + recursSum(startingNum + 1, numInts - 1);
}

Recursion, Proving that a statement involving an integer n is true for infinitely many We can efficiently compute the gcd using the following property, which holds The static method gcd() in Euclid.java is a compact recursive function whose integer 0 through n-1 in such a way that consecutive permutations differ in� To use recursion to add integers from first to last: The sum of the last integer on the list is itself: last. The sum of all the integers is the first integer added to the sum of the remaining integers. Here is a method that implements this.

Iterative approach:

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

Recursive Approach:

public static int recursSum(int startingNum, int numInts) {
        if (numInts < 0) {
            return startingNum;
        }
        return startingNum + recursSum(startingNum+1, numInts - 1);
}

Recursive Methods | Think Java, Up to this point, we've been using while and for loops whenever we've needed to repeat Figure 8.1 is a stack diagram for countdown , called with n == 3 . For example, here is a recursive method without a base case: Since factorial is defined for integers, the method takes an int as a parameter and returns an int :� Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iterations is known. It's because the number of iterations is known. The above programs don't work properly if the user enters a negative integer.

In your consecSum() you are updating the startingNum in the loop with startingNum += (startingNum + 1).

You should use a new variable for the result:

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

In your recursSum you are adding startingNum + numInts in each iteration. Just add 1:

public static int recursSum(int startingNum, int numInts) {
    if (numInts <= 0) {
        return startingNum;
    }
    return startingNum + recursSum(startingNum + 1, numInts - 1);
}

The result for (3, 3) in both cases is 18.

Python Tutorial: Recursive Functions, Recursion is a method of programming or coding a problem, in which a A recursion can end up in an infinite loop, if the base case is not met in the calls. The Fibonacci numbers are the numbers of the following sequence of integer values: Write a recursive program to calculate the Fibonacci numbers, using Pascal's� We generally use for loops when we know how many times we need to execute a block of code. In the above example, we only need to do lines 2 and 3 for the number of names in name_list .

5.3. Calculating the Sum of a List of Numbers — Problem Solving , The function uses an accumulator variable ( theSum ) to compute a running total of all the numbers in the list by starting with 0 and adding each number in the list. Run Pretend for a minute that you do not have while loops or for loops. Figure 1 shows the series of recursive calls that are needed to sum the list [1,3,5, 7,9]. Given two integers x and n where n is non-negative, efficiently compute the value of power function pow(x, n) using Divide & Conquer. A simple solution to calculate pow(x, n) would be multiply x exactly n times. We can do that by using simple for loop. The time complexity of this solution is O(n).

Print all possible consecutive numbers with sum N, Recommended: Please try your approach on {IDE} first, before moving on to the solution. One important fact is we can not find consecutive numbers above N/2 that Repeat the loop from bottom to half function findConsecutive( $N ) Minimum increments to convert to an array of consecutive integers� Using the .count() list method The .count() method is built-in to the list class, and unlike list expressions and filter() , we won’t be using an expression here.

Sum of natural numbers using recursion, To calculate the sum, we will use a recursive function recur_sum(). Please try your approach on {IDE} first, before moving on to the solution. Java Program to Find the Sum of Natural Numbers using Recursion In this program, you'll learn to find the sum of natural number using recursion in Java. This is done with the help of a recursive function.