Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pick one of the following programming languages: C, C++, Java, or Python. Implem

ID: 3682172 • Letter: P

Question

Pick one of the following programming languages: C, C++, Java, or Python. Implement the following three recursive functions

1. (5 points) The factorial function accepts a nonnegative integer as the argument n and returns the factorial value for the argument.

2. (5 points) The fibonacci function accepts a nonnegative integer as the argument n and returns the n-th fibonacci term.

3. (20 points) The Ackermann function accepts two nonnegative integers as the arguments (m, n) and returns a positive integer.

A requirement is that you need to output the value(s) of the current argument(s) whenever the function is invoked (one way of doing it is by putting an output statement in the very beginning of the function body, that prints out the argument value(s) before you do anything with the argument value(s)). You need to have a main function that tests (invokes) your functions. In particular, try the following set of arguments to test your functions.

1. The factorial function: invoke it with values 0, 1, 2, 3, 4, and 5.

2. The fibonacci function: invoke it with values 0, 1, 2, 3, 4, and 5.

3. The Ackermann function: invoke it with the following value pairs: (0, 0),(1, 1),(2, 2), and (2, 3).

Therefore, you will have one program that consists of the main function and the three recursive functions.

Another requirement is to include a README file that explains how to compile and run your program.  Also, include in the README file how the expected output would look like when your program is run.

Explanation / Answer

java program:

import java.io.*;
import java.util.Scanner;
public class Recursive {
public static void main(String []args)throws IOException{
for(int i=0;i<=5;i++){
System.out.println("Factorial of"+i+" is:"+Factorial(i));

}
for(int i=0;i<=5;i++){
System.out.println("Fibonacci of"+i+" is"+fibonacci(i));
}
System.out.println("Ackermann of (0,0) is:"+Ackermann(0,0));
System.out.println("Ackermann of (1,1) is"+Ackermann(1,1));
System.out.println("Ackermann of (2,2) is:"+Ackermann(2,2));
System.out.println("Ackermann of (2,3) is"+Ackermann(2,3));
}
public static int Factorial(int n){
if(n==0)
return 1;
return n*Factorial(n-1);
}
public static int fibonacci(int n) {

//Error condition:

if(n<0)
return -1;

//Base cases:
else if (n==0)
return 0;
else if (n==1)
return 1;

return fibonacci(n-1) + fibonacci(n-2);

}
public static int Ackermann(int m,int n){
if(m==0)
return n+1;
else if(m>0&&n==0)
return Ackermann(m-1,1);
else
return Ackermann(m-1,Ackermann(m,n-1));
}
  
}

output;

run:
Factorial of0 is:1
Factorial of1 is:1
Factorial of2 is:2
Factorial of3 is:6
Factorial of4 is:24
Factorial of5 is:120
Fibonacci of0 is0
Fibonacci of1 is1
Fibonacci of2 is1
Fibonacci of3 is2
Fibonacci of4 is3
Fibonacci of5 is5
Ackermann of (0,0) is:1
Ackermann of (1,1) is3
Ackermann of (2,2) is:7
Ackermann of (2,3) is9
BUILD SUCCESSFUL (total time: 0 seconds)