In: Computer Science
Fibonacci (C++)
Generate 1st n fibonacci numbers:
std::vector<int> v = {1, 1, 2, 3, 5, 8, 13, 21}; auto w = fibonacci(8);
Note: NO LOOPS ALLOWED
Thanks!
if you have any doubts please post an comment.
#include<iostream>
using namespace
std;
void printFibonacci(int n){
static int n1=1, n2=1,
n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<"
";
printFibonacci(n-1);
}
}
int main(){
int n;
cout<<"Enter the
number of elements: ";
cin>>n;
cout<<"Fibonacci
Series: ";
cout<<"1
"<<"1 ";
printFibonacci(n-2);
//n-2 because 2 numbers are already printed
return 0;
}
output:
input:8
1 1 2 3 5 8 13 21