
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Fibonacci Series
The Fibonacci sequence is like this,
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,……
In this sequence, the nth term is the sum of (n-1)'th and (n-2)'th terms.
To generate we can use the recursive approach, but in dynamic programming, the procedure is simpler. It can store all Fibonacci numbers in a table, by using that table it can easily generate the next terms in this sequence.
Input and Output
Input: Take the term number as an input. Say it is 10 Output: Enter number of terms: 10 10th fibinacci Terms: 55
Algorithm
genFiboSeries(n)
Input: max number of terms.
Output − The nth Fibonacci term.
Begin define array named fibo of size n+2 fibo[0] := 0 fibo[1] := 1 for i := 2 to n, do fibo[i] := fibo[i-1] + fibo[i-2] done return fibo[n] End
Example
#include<iostream> using namespace std; int genFibonacci(int n) { int fibo[n+2]; //array to store fibonacci values // 0th and 1st number of the series are 0 and 1 fibo[0] = 0; fibo[1] = 1; for (int i = 2; i <= n; i++) { fibo[i] = fibo[i-1] + fibo[i-2]; //generate ith term using previous two terms } return fibo[n]; } int main () { int n; cout << "Enter number of terms: "; cin >>n; cout << n<<" th Fibonacci Terms: "<<genFibonacci(n)<<endl; }
Output
Enter number of terms: 10 10th Fibonacci Terms: 55
- Related Questions & Answers
- Fibonacci Series in C#
- 8085 program to generate Fibonacci sequence
- 8086 program to generate Fibonacci Sequence
- C++ Program to Display Fibonacci Series
- JavaScript code for recursive Fibonacci series
- Java Program to Display Fibonacci Series
- Fibonacci series program in Java using recursion.
- Java program to print a Fibonacci series
- Nth element of the Fibonacci series JavaScript
- Fibonacci series program in Java without using recursion.
- Find fibonacci series upto n using lambda in Python
- Validate a number as Fibonacci series number in JavaScript
- Write a Golang program to print the Fibonacci series
- Python Program to Find the Fibonacci Series Using Recursion
- Java program to print Fibonacci series of a given number.
Advertisements