Thursday, December 5, 2013

Fibonacci Series

Fibonacci Series

This program will help you to print Fibonacci series on your console screen.. If you don't know about Fibonacci series it is something like this:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,......................

A new number in the series comes after the sum of last two numbers in the series like 0 + 1 = 1, so the third number is 1 and then 1 + 2 = 3, so the next number is three and so on...

Here is the output of the program I made:

Output

Code:


// Ahmad Mukhtar (www.cppfuzz.com)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
using namespace std;
void main()

{
 int num,c,i; // declaration
 int a=0,b=1,sum=0; // initiallization
 cout << "Enter a number: ";
 cin >> num; // This input will give us info that how many numbers of the series user wants to see
 for (i=0;i<num;i++)
 {
  if (i<=1)
  {
   c=i; // This condition is for printing 1 two times for series (0,1,1,2....)
  }
  else
  {
   c=a+b; // adds the last two numbers
   a=b; // assigns new number to the old variable
   b=c; // same as above
  }
  cout << c << "\t";
  sum = sum + c; // this is to print the sum of all the numbers of the series printed.
 }
 cout << endl;
 cout << endl;
 cout << "The sum of all the numbers is: " << sum;
 cout << endl;
}

Widgets

 

Copyright @ 2014 CPP Fuzz.