For each of the following recursion problems write the reque

For each of the following recursion problems, write the requested method and then a segment of code in a driver class that verifies that your solution is working correctly. Recall that for every recursive method you need to include the following three elements Base case Recursive call (one or more) Approach your base case with your recursive calls) Problems 1. Write a recursive method that accepts an integer n and prints the first n positive integers to the Java console. For example: n, (n 1), (n 2 3, 2, 1 For example, if n 3 10, then the following would be printed to the Java console 10 9 8 7 6 5 4 3 2 1 2. Write a recursive method that accepts an integer n and prints all multiples of 3 from n down to 3. For example, if was given n 20 en l would send the following output to the Java console: 18 15 12 96 3 Note: do not assume that n begins as a multiple of 3 3. Write a recursive method that accepts an integer n and returns the sum of the first n positive integers. 1 2 3 ...f (n-2) (n-1) n For example, if n 5. then the method should return 1 2 3 4 5 15

Solution

//Recursion.java


import java.util.Scanner;

public class Recursion
{

public static int recursion1(int n)
{
// base case
if(n == 1)
{
System.out.println(n);
return n;
}


else
{
System.out.print(n + \" \");
// recursive call
return recursion1(n-1);
}
}

public static int recursion2(int n)
{
// base case
if(n == 3)
{
System.out.println(n);
return n;
}


else
{
if(n%3 == 0)
System.out.print(n + \" \");

// recursive call
return recursion2(n-1);
}
}

public static int recursion3(int n)
{
// base case
if(n == 1)
{
return n;
}

else
{
// recursive call
return n+recursion3(n-1);
}
}

public static String recursion4(String str, int i)
{
// base case
if (i == str.length() -1 || i == str.length())
{
return str;
}

else
{
int t = str.indexOf(\' \');
// recursive call
System.out.println(str.substring(i, t));
i=i+t;
return recursion4(str.substring(i, str.length()), i);
}
}

public static void main(String[] args)
{

Scanner sc = new Scanner(System.in);

System.out.print(\"Enter n: \");
int n = sc.nextInt();

System.out.print(\"N positive integers: \");
int result = recursion1(n);

System.out.print(\"Multiples of 3: \");
result = recursion2(n);

System.out.print(\"Sum of first N positive integers: \" + recursion3(n) + \"\ \");

System.out.print(\"\ Enter a string: \");
String str = sc.next();
String s = recursion4(str, 0);

}

}

/*
Output:

Enter n: 20
N positive integers: 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Multiples of 3: 18 15 12 9 6 3
Sum of first N positive integers: 210

Enter a string: my name is
my
name
is

*/

 For each of the following recursion problems, write the requested method and then a segment of code in a driver class that verifies that your solution is worki
 For each of the following recursion problems, write the requested method and then a segment of code in a driver class that verifies that your solution is worki
 For each of the following recursion problems, write the requested method and then a segment of code in a driver class that verifies that your solution is worki

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site