Exercise: Fibonacci Numbers
Learn to write programs related to the Fibonacci number.
Fibonacci number
A sequence of numbers in which every number is the sum of the two preceding numbers, and the initial two values are constant as 0 and 1. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, etc.
This seemingly very simple sequence has in fact very fascinating applications! Starting with the number of petals on a flower, the number of spirals on a sunflower, the patterns on shells or on a pineapple, to the spiralling galaxies, all exhibit the Fibonacci number sequence!
Even the sum of squared Fibonacci numbers gives us Fibonacci numbers. We could keep going on and on about how amazing the Fibonacci numbers are, but let’s get to solving some problems related to the Fibonacci numbers and sequences.
Problem statement
Write a function that takes a parameter n
where n >= 0
and (returns) the nth
Fibonacci number.
Sample input
fibonacci(6)
Sample output
8
Implementation idea: Fibonacci
Let’s create a function prototype first:
int fibonacci(int nth)

Since we know for a fact that the sum of two preceding numbers gives the next Fibonacci number, we can maintain the two integer variables inside our function, namely
prev
andcurrent
to calculate thenext
Fibonacci number. 
Also, since first two numbers as 0, are
0
asprev
and1
ascurr
, we can writeifelse
statements that returnprev
orcurrent
ifn
is 0 or 1 respectively. 
In case
n
> 1, we can have afor
loop and do the following:
The initialization step in our
for
loop will start from 2 till thenth
number. 
To calculate the third Fibonacci number, we’ll add
prev
andcurrent
and assign them to the variablenext
.next = prev + current;

Now, to calculate the fourth Fibonacci number, we’ll need to update
prev
andcurrent
. The value inprev
should become the value incurr
, and the value incurr
should become the value innext
. 
The loop will run again for computing the next Fibonacci number.

This is repeated till the
nth
Fibonacci number is calculated.


After the loop, we return
curr
. becausecurr
will hold thenth
computed Fibonacci number.
Exercise: Fibonacci number
Now that you know how to implement the function, write its code in the code editor below.
int fibonacci(int nth){// Add code here.}
Kqueries on Fibonacci numbers
Let us modify the problem statement.
Problem Statement
Write a program that takes a number k
from the user where the number k
represents that the user wants to do k
queries to our Fibonacci function.
The program should prompt the user for each query and a number n
, and prints the nth
Fibonacci number. (Reuse the code above)
Sample input
Number of values (k) you want to know: 3
Sample output
Nth fibonacci number you want to know:20
F20 = 6765
Nth fibonacci number you want to know:2
F2 = 1
Nth fibonacci number you want to know:5
F5 = 5
Implementation idea: k
fibonacci number
For example, say k
is equal to 3, and the user wants to find the 6th, 3rd, and 4th Fibonacci numbers.
Then the program should print:
F6 = 8, F3 = 2, F4 = 3
Make changes in the code editor below to write this program.
For your convenience, we’ve already added the
fibonacci()
function that computes the Fibonacci of any numbern
.
Exercise: k
Fibonacci number
Write your code in the playground below and debug it step by step:
#include <iostream> using namespace std; int fibonacci(int nth); void fibonacciPrinting(int numberOfTerms); int main() { int k; cout << "Number of values (k) you want to know: "; cin >> k; fibonacciPrinting(k); return 0; } void fibonacciPrinting(int numberOfTerms) { // Add code here. } int fibonacci(int nth) { int prev = 0; int current = 1; int next; if(nth == 0) { return prev; } else if(nth == 1) { return current; } else { for(int i = 2; i <= nth; i++) { next = prev + current; prev = current; current = next; } return current; } }
Click “Show Solution” below to see the solution.
Fibonacci sequence
We’ve already seen that each number in the Fibonacci sequence is formed by adding the two preceding numbers. Let us now print the Fibonacci sequence up to a certain limit.
Problem statement
Write a program that takes a parameter t
and prints all the Fibonacci numbers less than t
.
Sample input:
Print the Fibonacci numbers till: 50
Sample output:
The sequence up to 50 is:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
For your convenience, we’ve already added the
fibonacci()
function that computes the Fibonacci of any number n.
Exercise: Fibonacci sequence
Write your code in the playground and debug it step by step:
#include <iostream> using namespace std; void seriesFibonacci(int n); int fibonacci(int nth); int main() { int t; cout << "Print the fibonacci numbers till: "; cin >> t; seriesFibonacci(t); return 0; } void seriesFibonacci(int n) { // Add code here. } int fibonacci(int nth) { int prev = 0; int current = 1; int next; if(nth == 0) { return prev; } else if(nth == 1) { return current; } else { for(int i = 2; i <= nth; i++) { next = prev + current; prev = current; current = next; } return current; } }
Click Show Solution below to see the solution.
Fibonacci sequence within a range
Let us now print the Fibonacci sequence present between two numbers.
Problem statement
Write a program that takes two parameters start and end and prints all the Fibonacci numbers between them.
Sample input:
Start: 3
End: 60
Sample output:
3, 5, 8, 13, 21, 34, 55
Exercise: Fibonacci sequence within a range
Write your code in the playground and debug it step by step:
#include<iostream> using namespace std; void rangeFibonacci(int start, int end); int main() { int rangeStart, rangeEnd; cout << "Start, End: "; cin >> rangeStart >> rangeEnd; rangeFibonacci(rangeStart, rangeEnd); return 0; } void rangeFibonacci(int start, int end) { // Add code here }
Click Show Solution below to see the solution.
Sum of even Fibonacci numbers
Let us now print the sum of even Fibonacci numbers up to a certain number.
Problem statement
By considering the terms in the Fibonacci sequence whose sum does not exceed a number, say fifty thousand, find the sum of only the evenvalued Fibonacci terms.
Sample input:
Sum of even Fibonacci numbers upto: 50000
Sample output:
Sum = 60696
For your convenience, we’ve already added the
fibonacci()
function that computes the Fibonacci of any number n.
Exercise: Sum of even Fibonacci numbers
Write your code in the playground and debug it step by step:
#include<iostream> using namespace std;//This program will tell the nth Fibonacci number int fibonacci(int nth);//It will return nth Fibonacci term int evenFibonacciSum(int number); int main() { int term; cout << "Sum of even Fibonacci numbers upto: "; cin >> term; cout << "Sum = " << evenFibonacciSum(term); return 0; } int evenFibonacciSum(int number) { // Write code here } int fibonacci(int nth) { int prev = 0; int current = 1; int next; if(nth == 0) { return prev; } else if(nth == 1) { return current; } else { for(int i = 2; i <= nth; i++) { next = prev + current; prev = current; current = next; } return current; } }
Click Show Solution below to see the solution.