Write a C++ program:to iteratively compute Fibonacci numbers (named after an Italian mathematician). Some Fibonacci numbers are 1,1,2,3,5,8,... where the next number is found by summing the preceding two numbers. Your program is not assured of recieving good data, thus if -2 is recieved as the number of numbers, then an error should be reported. Try n = 5, 7, 10, -3

Question
Asked Oct 28, 2019
31 views

Write a C++ program:

to iteratively compute Fibonacci numbers (named after an Italian mathematician). Some Fibonacci numbers are 1,1,2,3,5,8,... where the next number is found by summing the preceding two numbers. Your program is not assured of recieving good data, thus if -2 is recieved as the number of numbers, then an error should be reported. 

Try n = 5, 7, 10, -3

check_circle

Expert Answer

star
star
star
star
star
1 Rating
Step 1

A C++ program to generate fibonacci series using recursion is given below,

Program:

//Declare header files

#include<iostream>   

//Declare namespace

using namespace std;     

//Define the recursive function

void print_Fibonacci(int n)

{

    //Declare ans initialize the necessary variables

    static int n_1=0, n_2=1, n_3;   

    //Check whether "n" is positive

    if(n>0)

    {

        //Add two values

        n_3 = n_1 + n_2;   

        //Swap two numbers

        n_1 = n_2;   

        n_2 = n_3;   

        //Print the calculated value

        cout<<n_3<<" ";   

        //Call the function iteratively

        print_Fibonacci(n-1);   

    }   

}   

//Define the main() function

int main()

{

    //Declare the variable

    int n;   

    //Prompt the user for the input

    cout<<"Enter the number of elements: ";   

    //Get the user input

    cin>>n;   

    //Check whether "n" is less than or equal to "2"

    if(n<=2)

    {

        //Prin the error message

        cout<<"Try n=5,7,10";

    }

    else

    {

        //Prin the string

        cout<<"Fibonacci Series: ";  

        //Print "1"

        cout<<"1 ";

        //Call the function

       print_Fibonacci(n-1);  //n-1 because first number is already printed   

     }

     return 0; 

} 

Screenshot of program #1:

1/Declare header file
#include<iostream>
/Declare namespace
asing namespace std;
/Define the recursive function
void printFibonacci(int n)
//Declare and initialize the necessary variables
static int n_1=0, n_2=1, n_3;
//Check whether "n" is positive
if (n>0)
//Add two values
n_3 = n_1 n_2:
1/Swap two numbers
n 1 n 2;
n 3
n_2
help_outline

Image Transcriptionclose

1/Declare header file #include<iostream> /Declare namespace asing namespace std; /Define the recursive function void printFibonacci(int n) //Declare and initialize the necessary variables static int n_1=0, n_2=1, n_3; //Check whether "n" is positive if (n>0) //Add two values n_3 = n_1 n_2: 1/Swap two numbers n 1 n 2; n 3 n_2

fullscreen
Step 2

Screenshot of program #2:

//Print the calculated value
cout<<n_3<<" ";
//Call the function iteratively
printFibonacci (n-1);
}
//Define the main () function
int main
//Declare the variable
int n
//Prompt the user for the input
cout<<"Enter the number of elements:
/Get the user input
cin>>n
help_outline

Image Transcriptionclose

//Print the calculated value cout<<n_3<<" "; //Call the function iteratively printFibonacci (n-1); } //Define the main () function int main //Declare the variable int n //Prompt the user for the input cout<<"Enter the number of elements: /Get the user input cin>>n

fullscreen
Step 3

Screenshot of program #3...

//Check whether "n" is less than or equal to "2"
if (n<-2)
//Prin the error message
cout<<"Try n=5,7, 10";
else
{
//Prin the string
cout<<"Fibonacci Series:
/Print "1"
cout<<"1 ";
/Call the function
printFibonacci (n-l); //n-1 because first number is already printed
return 0
help_outline

Image Transcriptionclose

//Check whether "n" is less than or equal to "2" if (n<-2) //Prin the error message cout<<"Try n=5,7, 10"; else { //Prin the string cout<<"Fibonacci Series: /Print "1" cout<<"1 "; /Call the function printFibonacci (n-l); //n-1 because first number is already printed return 0

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: what are the mantissa and exponent value of 6.75 in 8 bit float resperation

A: First, we convert the number into binary representation. So 6.75 to base 10 is equal to 110.110 to b...

question_answer

Q: How many lines does his algorithm print? Write a recurrence and solve it.function printaton(n: an in...

A: The given program splits into three subprograms with half of the input size each and then prints n^4...

question_answer

Q: Assume that a double variable named alpha contains the value of an angle in radians. Compose a C++ s...

A: Below program uses following c++ functionsstd::setw, std:setfill, setprecision(int n) defined under ...

question_answer

Q: I need help with this programming exercise:   Write a C++ console application that allows your user ...

A: Program code #1:

question_answer

Q: Write a program in Python that converts an improper fraction to a mixed number. The program should p...

A: Program:#Display the messageprint("Calculate Mixed Number")print("======================")print()#Pr...

question_answer

Q: Question 1: What does the following code output? "Hello you, this is not a robot"; string str1 = str...

A:  Program explanation: The String variable “str1” is initialized to “Hello you, this is not a robot”....

question_answer

Q: What are the benefits of using a vector over an array?

A: Vector - It is a data structure used to store data.Vectors can be implemented as dynamic arrays with...

question_answer

Q: 3. Use a trace table to show what is the output of the following program? #include using namespace ...

A: As per the policy we are required to answer only one question.Solution for question 3 is as follows:...

question_answer

Q: What folder contains group policy templates, logon/logoff scripts, and DFS synchronization data? Gro...

A: “System Volume (SYSVOL)” folder contains group policy templates, logon scripts, and Distributed File...