Question

Please write code in C++ using recursive function Write a program that computes the sequence of...

Please write code in C++

using recursive function

Write a program that computes the sequence of Fibonacci numbers. The formula for generating the next Fibonacci number is: Fn = Fn−1 +Fn−2, where F1 = 1 and F2 = 2. For example, F3 = F2 + F1 = 2 + 1 = 3. You will notice that at some point Fibonacci numbers are too large and they do not fit in type int. This is called the integer overflow. When they become negative (or nonincreasing), stop the program. Please use three different functions, one for each type.

• How many Fibonacci numbers fit in type int? What is the value of the largest one?

• How many Fibonacci numbers fit in type long? What is the value of the largest one?

• How many Fibonacci numbers fit in type long long? What is the value of the largest one?

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

Code For fibonacci using resursion

Normal For Type Int

#include<bits/stdc++.h>

using namespace std;

// This depicts the function calculation for fibonacci series .

// We get series by summing up last two numbers i.e if we want nth fibonaci number which means we add n-1 and n-2 number to get the series .

int fibonacci(int n)

{

    if (n <= 1)

        return n;

int ans= fibonacci(n-1) + fibonacci(n-2);

if(ans <0)

{

return -1;

}

else

{

return ans;

}

}

  int main ()

{

    int n = 9 ,output;

     output=fibonacci(n);

if(output==-1)

{

cout<<"Integer Overflow";

}

else

{

cout<<"Output';

}

    return 0;

}

So the largest number of fibonaaci in 32 unsigned bit integer is 46 we can compute without getting overflow is so fibonacci(46) it goes out of range  after that and start printing negative

For type long

long fibonacci(int n)

{

    if (n <= 1)

        return n;

long ans= fibonacci(n-1) + fibonacci(n-2);

if(ans <0)

{

return -1;

}

else

{

return ans;

}

}

So the largest number of fibonaaci in 32 unsigned bit long is 47 we can compute without getting overflow is fibonacci(47) as it goes out of range after that and start printing negative

For type long long

long long fibonacci(int n)

{

    if (n <= 1)

        return n;

long long ans= fibonacci(n-1) + fibonacci(n-2);

if(ans <0)

{

return -1;

}

else

{

return ans;

}

}

So the largest number of fibonaaci in 32 unsigned bit long long is 93 we can compute without getting overflow is fibonacci(93) as it goes out of range after that and start printing negative .

Thank You Please Upvote IF U like the answer.

Add a comment
Know the answer?
Add Answer to:
Please write code in C++ using recursive function Write a program that computes the sequence of...
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...

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

  • c++ program Write a recursive function that computes and returns the product of the first n...

    c++ program Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • The Fibonnaci sequence is a recursive sequence defined as: f0 = 1, f1 = 1, and...

    The Fibonnaci sequence is a recursive sequence defined as: f0 = 1, f1 = 1, and fn = fn−1 + fn−2 for n > 1 So the first few terms are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .. Write a function/procedure/algorithm that computes the sum of all even-valued Fibonnaci terms less than or equal to some positive integer k. For example the sum of all even-valued Fibonnaci terms less than or equal to 40...

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

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • ARM assembly language Write a program "fibonacci.s" that computes the Nth Fibonacci number where N is...

    ARM assembly language Write a program "fibonacci.s" that computes the Nth Fibonacci number where N is not so large that overflow of integer arithmetic is a concern. When your assembly language program is called it should expect the value of N to be passed using register r0 and your program should return the Nth Fibonacci number in register r0. Please include comments as well. Do not just use the output generated by gcc -S

  • Please write and draw the recursive trace, and please explain, thank you! Problem 1. [26 pts]...

    Please write and draw the recursive trace, and please explain, thank you! Problem 1. [26 pts] Given the Fibonacci numbers, defined as: Fo 0, F1-1, F k Fk2 write or draw the recursive trace of the calculation of the 5th Fibonacci number (Fs 5) with the following Algorithm (which is based on linear recursion) Algorithm Fibonacci Linear (k) Input: a positive integer value k Output: a pair of Fibonacci numbers (F., F..) if k < 2 then R-1 return (R,...

  • Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int);...

    Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int); int main() { f1(10); return 0; } void f1(int n) { f2(n + 5); } void f2(int n) { f3(n - 2); } void f3(int n) { cout << n << endl; // LINE 1 } Just before the program statement marked with the comment "LINE 1" is executed, how many stack frames will be on the program call stack?

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