Question

In: Computer Science

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

(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25.
Your method must use recursion.

Solutions

Expert Solution

Explanation:I have implemented the method sumForEachOther() recursively, I have also provided a main method in a RecursiveSum class to show the output of the program, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//method

public static int sumForEachOther(int n) {
       if (n < 1) {
           return 0;
       }
       return n + sumForEachOther(n - 2);

   }

//demo code


public class RecursiveSum {

   public static int sumForEachOther(int n) {
       if (n < 1) {
           return 0;
       }
       return n + sumForEachOther(n - 2);

   }

   public static void main(String[] args) {
       int sum = sumForEachOther(8);
       System.out.println(sum);
       sum = sumForEachOther(9);
       System.out.println(sum);
   }

}

Output:


Related Solutions

Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a method with the signature public static NaturalNumber divRemainder(NaturalNumber m, NaturalNumber n) The method takes...
Write a method with the signature public static NaturalNumber divRemainder(NaturalNumber m, NaturalNumber n) The method takes two NaturalNumbers as arguments and should return a new NaturalNumber whose value is the sum of m divided by n and the remainder of m divided by n. For example, if m = 23 and n = 6, the method should return a NaturalNumber with the value 8, since 23/6 + 23 rem 6 = 3 + 5 = 8. Do not use toInt()...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT