Answer to Question #345501 in C++ for Umi

Question #345501

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
2022-05-30T08:15:29-0400
#include <iostream>
using namespace std;
void printFibonacciNumbers(int n)
{
    int f1 = 0, f2 = 1, i;
    if (n < 1)
        return;
    cout << f1 << " ";
    for (i = 1; i < n; i++) {
        cout << f2 << " ";
        int next = f1 + f2;
        f1 = f2;
        f2 = next;
    }
}
int main() {
    int n;
    cout<<"Enter the number of elements of Fibonacci series to print: ";
    cin>>n;
    printFibonacciNumbers(n);
    return 0;
}

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
APPROVED BY CLIENTS