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

Write a program that uses recursive methods to compute and output the nth term o

ID: 3816679 • Letter: W

Question

Write a program that uses recursive methods to compute and output the nth term of the Fibonacci series, with n being input by the user. The program should repeatedly ask the user the term number to be computed, and produce two lines of output per term each containing the term number, followed by the term’s value, followed by the number of times the recursive method is invoked to calculate the term's value.

The first line of output will be computed using a dynamic recursive method, and the secondline of output will be computed using a recursive method that utilizes non-dynamic programming to reduce the execution time of the program. Be sure the dynamic version of the method executes in under 0.1 seconds for values of n > 45.

Hint: the computed and returned values should be of type long, because the computed values become too large for type int. F100 = 3,736,710,778,780,434,371

Java is the programming language

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class fib
{
public static void main(String[] args) throws IOException
{
System.out.print(" Enter the value of n : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
System.out.println(" "+n+"th value in fibonacci series is : "+fib(n));

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

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote