Fibonacci Algorithm
- The Fibonacci series is a sequence of numbers in the following order: The numbers in this series are going to start with 0 and 1. The next number is the sum of the previous two numbers. The formula for calculating the Fibonacci Series is as follows: T(n) = T (n-1) + T(n-2) where: T(n) is the term number.
0,1,1,2,3,5,8,13,21,and so on |
Fibonacci series example. |
Code for fibonacci series in c++:
#include <iostream>
using namespace std;
void fib(int n){
//void function
int t1=0; //term 1
int t2=1; //term 2
int nextterm;
for(int i=1;i<=n;i++){
cout<<t1<<" ";
nextterm=t1+t2;
t1=t2;
t2=nextterm;
}
}
int main() {
int n;
cin>>n; //input n
fib(n); //function call
return 0;
}
Instagram 👇
👍
ReplyDelete