What is fibonacci series?
Fibonacci series is a series formed by adding previous two numbers. Suppose you have given first number= 0 and second number=1 then your fibonacci series will be
Program:
#include <iostream>
using namespace std;
//function to write fibonacci series
void fibo(int n)
{
int n1=0,n2=1,next;
//loop to calculate and print series
for(int i=1;i<=n;i++)
{
cout<<n1<<" ";
next=n1+n2;
n1=n2;
n2=next;
}
}
int main()
{
int n;
cout<<"Enter number of terms: ";
cin>>n;
fibo(n); //function call
return 0;
}
Output:
Enter number of terms: 5
0 1 1 2 3
C++ Program to print fibonacci series using function
Reviewed by Beast Coder
on
January 18, 2021
Rating:
No comments: