Question

Write a recursive function that finds the n-th integer of the Fibonacci sequence(in C++ using a...

Write a recursive function that finds the n-th integer of the Fibonacci sequence(in C++ using a function). Then build a minimal program to test it. For reference see Fibonacci number. To check for recursion, please have the Fibonacci function print out its input as shown in the examples below:

INPUT:

5

OUTPUT:

fib(5)  fib(4)  fib(3)  fib(2)  fib(1)  fib(0)  fib(1)  fib(2)  fib(1)  fib(0)  fib(3)  fib(2)  fib(1)  fib(0)  fib(1)  
5

INPUT:

7

OUTPUT:

fib(7)  fib(6)  fib(5)  fib(4)  fib(3)  fib(2)  fib(1)  fib(0)  fib(1)  fib(2)  fib(1)  fib(0)  fib(3)  fib(2)  fib(1)  fib(0)  fib(1)  fib(4)  fib(3)  fib(2)  fib(1)  fib(0)  fib(1)  fib(2)  fib(1)  fib(0)  fib(5)  fib(4)  fib(3)  fib(2fib(1)     fib(0)  fib(1)  fib(2)  fib(1)  fib(0)  fib(3)  fib(2)  fib(1)  fib(0)  fib(1)  
13
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN C++:

#include <iostream>

using namespace std;
int fib(int n ){
cout << "fib(" << n << ") " ;
if(n==1 || n==2){
  
return 1 ;
}
else{
return fib(n-1) + fib(n-2);
}
}

int main()
{
int n ;
cout << "Enter an integer : " ;
cin >> n ;
int result = fib(n);
cout << "" << endl ;
cout << result << endl ;

return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a recursive function that finds the n-th integer of the Fibonacci sequence(in C++ using a...
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
  • Below you will find a recursive function that computes a Fibonacci sequence (Links to an external...

    Below you will find a recursive function that computes a Fibonacci sequence (Links to an external site.).   # Python program to display the Fibonacci sequence up to n-th term using recursive functions def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # Change this value for a different result nterms = 10 # uncomment to take input from the user #nterms = int(input("How many terms? ")) # check if the number...

  • 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)

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int...

    In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...

  • in C++ 6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5,...

    in C++ 6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5, 8, 13, 21, 34, 55, 89.. 1 See the pattern? Each element in the series is the sum of the preceding two items. There is a recursive formula for calculating the nth number of the sequence (the oth number if Fib(0)-0): 8 Fib(N)-/N, if N 0 or 1 ifN> 1 Fib(N-2) Fib(N-1), a. b. c. Write a recursive version of the function Fibonacci. Write...

  • Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to...

    Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...

  • C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a...

    C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a simple pattern: 1,1, 2, 3, 5, 8, 13, 21, 34, 55, By definition, the first two numbers are 1 and 1, and each subsequent number is the sum of the previous two. For your program, you will ask the user to enter a number, n, and then calculate and display the values of the Fibonacci sequence to the nth position. ==sample output== Enter an...

  • I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a...

    I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a program that uses a for loop to calculate a Fibonacci sequence (NOT RECURSIVE!!!) up to a given position, starting with position 0. This function defines a Fibonacci sequence: If the number is 0, the function returns a 0 If the number is 1, the function returns a 1 If the number is higher than 1, it returns the sum of the previous two numbers...

  • In Java: The Fibonacci sequence is a series of numbers beginning with 0 and 1, in...

    In Java: The Fibonacci sequence is a series of numbers beginning with 0 and 1, in which each succeeding number is the sum of the previous two.     0, 1, 1, 2, 3, 5, 8, 13, 21, …. Practice your knowledge of recursion by producing a program that prints the nth Fibonacci number. That is, the program should accept an integer (n) as input and output the number that is the nth number in the Fibonacci sequence. For example, if n...

  • CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will...

    CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will be working with arrays and multiple function calls, including a recursive function call. The goal of the assignment will be to, using a pre-set array of sequence numbers, calculate the Fibonacci sequence number for each value in the array (see the example at the bottom if this isn’t clear). Instructions and Requirements: • Create a program that assembles and runs to find the correct...

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