C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Friday, January 10, 2014

Explanation of Fibonacci Series n Terms Program

Q:Write a C++ program to input a number N and display N fibonacci Terms.

Program Planning: Printing n Terms of Fibonacci Series

We start the logic building about the above mentioned Fibonacci series program. First of all we try to understand what the Fibonacci Series is:

What is a Fibonacci Series?

Fibonacci Series is a series of numbers where every next number is obtained by adding the two previous numbers. Note that first two numbers are 0 and 1 by definition. So the Fibonacci Series will be as under:

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and so on.

Term No 1 2 3 4 5 6 7 8 9 10
Term 0 1 1 2 3 5 8 13 21 34
For example, if we wish to write Nth term of the series, then we will get nth term by adding (n-1)th and (n-2)th term.

Nth Term = (n-1)th Term + (n-2)th Term

For example, if we wish to find 5th term

5th Term  = (5-1)th + (5-2)th Term

                 = 2          +           1

                 = 3

Easyway how to write C++ Program Logic: N Terms of Fibonacci Series in C++

Easyway how to write C++ Program Logic: N Terms of Fibonacci Series in C++

 

Program Logic: N Terms of Fibonacci Series in C++

1. We know that First Two Terms are 0 and 1, so initialize First=0 and Second=1
2. Print the value of first and second term by cout<<
3. If we find n terms then we know that we have already displayed two terms. Therefore, we will use a while loop to display remaining n-2 terms. In loop body we will add first and second to get third term.
4. Now we will place the value of second term in first. We will also place value of  third term in second.


Program Coding: N Terms of Fibonacci Series in C++

/*
(c)EasyCppProgramming.Blogspot.Com
C++ is Simplified Here!
Target 1001 C++ Example Programs!
EasyCppProgramming.Blogspot.Com
*/

// Program To Input a Number n and
// display n terms of Fibonacci Series
// by while loop
#include<iostream>
#include<conio.h>
#include<stdlib.h>


 int main()                 
 {
   int first=0, second=1, third, count=2, n;
   clrscr();
   cout<<"How Many Terms of Fibonacci Series? ";
   cin>>n;
   if (n<1)
      {
       cout<<"\n Invalid Input, Enter a Positive number";
       getch();
       exit(0);
      }

   if (n==1)
       cout<<first;
   else if (n==2)
       cout<<first<<"\t"<<second;
   else
   {
      cout<<first<<"\t"<<second;
    while(count<n)
      {
        third = first + second;
        cout<<"\t"<<third;
        first = second;
        second = third;
        count++;
      }
   }
   getch();
   return 0;
 }

Further reading suggestions:

Basic Structure of a C++ Program

C++ Program Development Cycle

How To Write, Compile and Run Your First C++ Program

How To Start Turbo C++ IDE

How To Install Turbo C++ 3.0 - IDE

Features of C++ Programming Language

Brief History of C++

Flowcharts in Computer Programming

The Role of Algorithms in Programming

How To Start Computer Programming
What is a Computer Program
- See more at: http://easycppprogramming.blogspot.com
Share:

2 comments:

  1. Nice post!!thanks for sharing good post.java vogue have good collection for improve program skill visit Programming Questions And Answers

    ReplyDelete

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Labels