Recursion exercises Download

Victor Morgan

recursion, basic map, string manipulation Student approved


Introduction

Write each of the methods below using recursion.


Task

Write methods that print:

  1. Numbers from int n down to 0 on new lines
  2. Each letter of String s on a new line in reverse order
  3. The sum of al positive integers less than or equal to int n
  4. The average of the elements in int[] nums. (int index will help you keep track of your position in the array.)


Starter code

class Recurse(){
	static int countDown(int n){

	}

	static String printRev(String s){

	}

	static int sumLessThan(int n){

	}

	static double average(int[] nums, int index){

	}
}

Expected output

// countDown(5)
5
4
3
2
1
0

// countDown(3)
3
2
1
0

// printRev(“abcdef”)
a
b
c
d
e
f

// printRev(“abc”)
a
b
c

// sumLessThan(4)
10

// sumLessThan(5)
15

// average({3,2,1}, 0)
2

// average({3,2,2}, 0)
2.333333

Useful tools to solve the problem