Fibonacci series from 1 (All terms using loop) – CSKC
Sign In

Search

Code Learning #CPP#2392    siteicon   siteicon  

Fibonacci series from 1 (All terms using loop)

Printing 10 terms of Fibonacci series  starting from 1 like 1 1 2 3 5 8 13 21 34 55 . All terms are printed using the loop.

Learning Objectives

  • Creating the Fibonacci series, the ancient mathematics style which starts from 1.
  • All terms are to be printed using the loop in a generic manner.

Source Code

TC++ #2392

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

Code Understanding

int term=1,pterm=0, ppterm;
Here first we set term=1 which shall be directly printed. Immediately previous term or pterm we set at 0 as the next term has to be 1 again. Previous to previous term which is declared as ppterm here will not be required to set in the beginning as it will be set in the loop automatically.

for(int tc=1;tc<=no_of_terms;++tc) //This works from 1 to no_of_terms.
{
cout<<term<<” “;  //The term is printed first.
ppterm=pterm;          //previuos term is shifted to previous-previous term (ppterm) location
pterm=term;              //current term is shifted to previous term (pterm) location.
term=pterm+ppterm; //Two previous terms are added to create the new term for next iteration.
}

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 modern style of Fibonacci series additional initial term 0 has also been added so the series becomes 0 1 1 2 3 5 8 13 21 34 ….

Common Errors

  • Fixing correct initial values is extremely important to run this program.


Suggested Filename(s): fibo-112.cpp



Share

Created: 9-Jan-2018 01:39 | Updated: 15-Sep-2018 12:49|