Answer to Question #203111 in C++ for Rabia akbar

Question #203111

Consider a situation that a user wants to print first n (any value, e.g. first 10) numbers of Fibonacci series. You need to implement a program that gets a number from user and prints first n Fibonacci series numbers. Write a function for this purpose that prints Fibonacci series recursively. This function should accept a number in parameter and then print n number of elements of Fibonacci series.



1
Expert's answer
2021-06-05T02:13:28-0400
#include <iostream>


using namespace std;
int Fibonacci(int n);


int main() {
	int n;
	//gets a number from user 
	cout<<"Enter n: ";
	cin>>n;
	//prints first n Fibonacci series numbers. 
	for(int i = 0; i < n; i++){
       cout<<Fibonacci(i)<<" ";
    }
	cout<<"\n\n";
	system("pause");


	return 0;
}


int Fibonacci(int n){ 
    if(n == 0 || n == 1)
    {
        return n;
    }
    else
    {
        // recursive call
        return Fibonacci(n-1) + Fibonacci(n-2);
    }


}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog