Question

Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...

Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1.

For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30.

The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25.

Your method must use recursion.

Homework Answers

Answer #1

public static int sumEveryOther(int n) {
    if (n > 0) {
        return n + sumEveryOther(n - 2);
    }
    return 0;
}

public class SumEveryOther {

    public static int sumEveryOther(int n) {
        if (n > 0) {
            return n + sumEveryOther(n - 2);
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(sumEveryOther(10));
        System.out.println(sumEveryOther(9));
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
Write a recursive method body for the method whose contract is given below. Note that n...
Write a recursive method body for the method whose contract is given below. Note that n is a restores-mode parameter. As an example, if n = 314, the method should return 1. You can only use the NaturalNumber kernel methods multiplyBy10, divideBy10, and isZero. /** * Reports the minimum (i.e., smallest) digit of n when it is * written in ordinary decimal notation. * ... * @ensures * minDigit = [the minimum digit of n] */ private static int minDigit(NaturalNumber...
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion *...
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion * I'm guessing we need to modify it Here's the code unmodified public class Sum { //Non recursive sum public static long sum1 (int n) { long sum = 1L; for (int i = 2; i <= n; ++i) sum = sum + i *i ; return sum; } //Recursive sum public static long sum2 (int n) { if (n < 2)return 1L; return sum2(n...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)          ...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3,...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
Please code in Java Write a recursive method that takes a string and return a string...
Please code in Java Write a recursive method that takes a string and return a string where every character appears twice. For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it.
Write down the run time of the following recursive functions as a recurrence relation: int f(...
Write down the run time of the following recursive functions as a recurrence relation: int f( int n ) { if ( n <= 1 ) { return 1; } return f( n – 1 ) + f( n – 1 ); }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT