Fibonacci series from 0 (Fixed first two terms)
Printing 10 terms of Fibonacci series starting from 0 like 0 1 1 2 3 5 8 13 21 34 . First two terms are printed as fixed terms and rest all terms are printed using the loop.
Learning Objectives
- Creating the Fibonacci series, the modern mathematics style which starts from 0.
- The logic with fixed first two terms and then later looping for addition of two previous terms is done.
Source Code
|
|
Related ( ? ) : |
Run Output
Code Understanding
int ppterm=0,pterm=1,term;
Here first two terms are manually printed as fixed terms as we analyse that addition logic of two previous terms will be easier to build when first two terms are present. So ppterm is set as 0 and pterm is set as one. This means the term which is previous term (pterm) with reference to forthcoming third term is 1 and previous to previous (ppterm) term is 0. This is obviously the case when the output is like 0 1 1 2 3 5 8 13 ……
cout<<ppterm<<” “<<pterm<<” “;
First two terms are directly printed. This is done so that existence of first two terms is guaranteed and third term on wards the loop can tun successfully adding just two previous terms.
for(int tc=3;tc<=no_of_terms;++tc)
In this case the loop begins from third term and goes to no_of_terms.
{
term=pterm+ppterm; //The third term calculation is addition of two previous terms.
cout<<term<<” “; //The calculated term is printed.
ppterm=pterm; //previuos term is shifted to previous-previous term (ppterm) location
pterm=term; //current term is shifted to previous term (pterm) location.
}
Notes
- The concept of series with addition of two previous numbers originated in India around 200 BC, around 1400 years before it was discussed by western Mathematician Fibonacci, who also popularised the Hindu-Arabic numeral system of counting with symols 0 to 9 in the west.
- In the ancient style of Fibonacci series it starts from 1 as 1 1 2 3 5 8 13 21 34 55 …
Common Errors
- Fixing correct initial values is extremely important to run this program.
Suggested Filename(s): fibo-011.cpp
Created: 9-Jan-2018 17:58 | Updated: 15-Sep-2018 12:55|
