Question

Write the code in java programming language To get some practice with recursion. You can do...

Write the code in java programming language

To get some practice with recursion.

You can do all this in one driver program.

Write a recursive method to compute the result of the Fibonacci sequence:

Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2)

N == 0 is 0

N == 1 is 1

Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Importing for Scanner Class

import java.util.*;

public class Main

{

// Function computing the Result of the Fibonacci sequence

static int Fibonacci(int N)

{

// In case N = 0 or N = 1 it returns the same N otherwise compute

// Computation done by adding the previous two terms

if (N>1)

{

return(Fibonacci(N-1)+Fibonacci(N-2));

}

else

{

return N;

}

}

public static void main(String[] args)

{

// Object Instantiation of scanner class

Scanner s = new Scanner(System.in);

System.out.print("Enter the number:");

// User entering the number for Computation

int a = s.nextInt();

System.out.println("Result of the Fibonacci sequence:");

// Printing the computation result

System.out.println(Fibonacci(a));

System.out.println("Number of function calls of Fibonacci sequence:");

// Printing the number of calls required using the generic term

System.out.print(2*Fibonacci(a)-1);

}

}

Code snapshot:-

// Importing for Scanner Class import java.util.*; public class Main // Function computing the Result of the Fibonacci sequenpublic static void main(String[] args) // Object Instantiation of scanner class Scanner s = new Scanner(System.in); System.ou

Output snapshot:-

Enter the number:2 Result of the Fibonacci sequence: Number of function calls of Fibonacci sequence:

Enter the number:5 Result of the Fibonacci sequence: Number of function calls of Fibonacci sequence:

Enter the number:10 Result of the Fibonacci sequence: Number of function calls of Fibonacci sequence: 109

Add a comment
Know the answer?
Add Answer to:
Write the code in java programming language To get some practice with recursion. You can do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions...

    Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive a zero for that problem....

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives...

    Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive...

  • Please help with this coding problem, and use the ARM sim# to test run your code....

    Please help with this coding problem, and use the ARM sim# to test run your code. Implement functions in assembly language. You are asked to write a program to compute the Fibonacci numbers, using recursive function calls. Fib(n){if (n == 0 || n == 1) return 1; else return Fib(n-2) + Fib(n-1);}

  • Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a...

    Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a high level language of your choice and in Assembly to find a Fibonacci number and check your result. At the end of this assignment a possible solution to this problem is given both in Python and MIPS assembler. Note that it would have been possible to write the Python differently to achieve the same function. . [20 points] Write and debug a MIPS program...

  • write in C language In = { 3.3 The Jacobsthal Function The Jacobsthal sequence is very...

    write in C language In = { 3.3 The Jacobsthal Function The Jacobsthal sequence is very similar to the Fibonacci sequence in that it is defined by its two previous terms. The difference is that the second term is multiplied by two. 0 if n=0 if n = 1 Jn-1 + 2Jn-2 otherwise Write a recursive function to compute the n-th Jacobsthal number. Write a main function that reads in an integer n and outputs Jn. Test your program for...

  • Please write down the code in JAVA. Objectives .To gain experience with using recursion by implementing...

    Please write down the code in JAVA. Objectives .To gain experience with using recursion by implementing the Ackermann's Function. To gain experience with using class variables to record method invocation history Description The Ackermann's function is of interest because it grows rapidly with respect to the size of m and n. It is the simplest example of a well-defined total function which is computable but not primitive recursive. This means it cannot be implemented using only for-loops. Notice that for-...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • (20 pts) To understand the value of recursion in a programming language: implement the binary search...

    (20 pts) To understand the value of recursion in a programming language: implement the binary search algorithm first as a recursive function and again using a conditional loop. Your program should create an array of characters holding the letters ‘A’ – ‘Z’ and find the index in the array where the letter ‘K’ is stored. You may use any programming language that supports recursion. (5pts) Define syntax and semantics and give an example. (5pts) Why is it important for a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT