Question

Language : Java

2. Write a recursive method, addRecur, to calculate the total of a number n. For example, if n 5, the total is 5+4+3+2+1-15 o

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

class Main {
public static int addRecur(int n, int sum) {
if(n==0) return sum;
else return addRecur(n-1, sum+n);
}
   public static void main (String[] args) {
       int n = 5;
       int ans = addRecur(n, 0);
       System.out.println("Answer: "+ans);
   }
}

OUTPUT:

Answer: 15
Add a comment
Know the answer?
Add Answer to:
Language : Java 2. Write a recursive method, addRecur, to calculate the total of a number n. For example, if n 5, the total is 5+4+3+2+1-15 or if n-0, the total is 0. Use the following definition to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • python language. [27] Write a recursive function to calculate the following for a given input value...

    python language. [27] Write a recursive function to calculate the following for a given input value for x. result = 1 + 2 + 3 .... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.

  • Using your choice of language, C#, Java, or pseudocode, write a recursive method that receives only...

    Using your choice of language, C#, Java, or pseudocode, write a recursive method that receives only one parameter, an int n, and prints the cubed of the numbers from 1 to n3 in ascending order. Do not use any global variables or stacks. For example, passing this method the value 5, the output would be “1    8    27    64    125”. Because 13 = 1, 23 = 8, 33 = 27, 43 = 64 and so on

  • In Java Write a recursive method called sumUpto(n) which will return the sum of the first...

    In Java Write a recursive method called sumUpto(n) which will return the sum of the first n integers. For example, sumUpto(4) will return 10 because 1+2+3+4 = 10. sumUpto(5) will return 15 because 1+2+3+4+5=15. So you can see that sumUpto(5) = sumUpto(4) + 5. Make this more general for n : sumUpto(n) = sumUpto(??) + n Figure out the base case and write the method……….. import java.util.Scanner; public class Lab12Num1 { public static int sumUpto(int num) {    } public...

  • Write a JAVA recursive method to calculate an exponent. Your method should have the following header....

    Write a JAVA recursive method to calculate an exponent. Your method should have the following header. Note: you can write a second, helper method if you choose. (Example: if base = 2 and power = 4, your method should return 2 ^ 4 = 16.) public int calculateExponent(int base, int power)

  • Language should be java Create a recursive function, int combinations (int n, int k) to calculate...

    Language should be java Create a recursive function, int combinations (int n, int k) to calculate the number of ways to select k items from n items. Use this formula: morfin.. C(n, k) = { 0 if k=0 if n <k otherwise C(n-1, k-1) + C(n-1,k) Running your program should look like this: Enter an Integer: 6 Enter another Integer: 2 combinations (6,2) = 15 Enter an Integer: 8 Enter another Integer: 4 combinations (8,4) = 70

  • The Abo series is the following: 0, 1, 2, 4, 3, 6, 5, 5, 4, 8,...

    The Abo series is the following: 0, 1, 2, 4, 3, 6, 5, 5, 4, 8, 7, 7, 6, 7, 6, 6, 5, 10... This series is defined in the following manner: Abo(n) = 0 for n <= 0 Abo(1) = 1 Abo(n) = 1 + Abo(n/2) if n > 1 is even Abo(n) = 2 + Abo((n+1)/2) if n > 1 is odd Write a recursive method that computes Abo(n). Write a program that prints the first 20 Abo...

  • Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number....

    Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number. Fibonacci numbers (or a Fibonacci sequence) are a series of numbers with a property that the next number in the series is a sum of previous two numbers. Starting the series from 0, 1 as the first two numbers we have 0, 1, (0 + 1) = 1, (1 + 1) = 2, (1 + 2) = 3, (2 + 3) = 5, (3...

  • Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number...

    Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number which is defined as follows: fibo(0) = 0 fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2) for n >= 2 Question 2. Write a recursive function that calculates the sum of quintics: 1power of5 + 2power of5 + 3power of5 + … + n5 Question 3. Write a program to find a route from one given position to another given position for the knight...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT