Google PlusFacebookTwitter

Constructors – Fibonacci Series

By on Apr 21, 2007 in On A Whim | 2 comments

Share On GoogleShare On FacebookShare On Twitter

/* Copyright © 2007 Ankur Banerjee. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html */

/* Constructors – Fibonacci Series */

#include <iostream.h>
#include <conio.h>

class fib
{
int f, s, n;
public:
fib()
{
f = 0;
s = 1;
n = 10;
}
fib(int first, int second, int number)
{
f = first;
s = second;
n = number;
}
void gen_fib();
} ;

void fib::gen_fib()
{
int t;
cout<<endl<<“Generating required Fibonacci series \n \n”
<<f<<” “<<s<<” “;
for(int i = 0; i < (n – 2); i++)
{
t = f + s;
cout<<t<<” “;
f = s;
s = t;
}
}

void main()
{
clrscr();
int ch, ft, sc, num;
cout<<endl<<“Program Menu”<<endl
<<“1. Generate default Fibonacci series”<<endl
<<“2. Generate custom Fibonacci series”<<endl
<<“Enter your choice : “;
cin>>ch;
switch(ch)
{
case 1 : fib f1;
f1.gen_fib();
break;
case 2 : cout<<“\nEnter first term of Fibonacci series : “;
cin>>ft;
cout<<“Enter second term of Fibonacci series : “;
cin>>sc;
cout<<“Enter number of terms : “;
cin>>num;
fib f2(ft, sc, num);
f2.gen_fib();
break;
default : cout<<endl<<“You entered an incorrect choice”;
}
getch();
}

/* Output */

Program Menu
1. Generate default Fibonacci series
2. Generate custom Fibonacci series
Enter your choice : 1

Generating required Fibonacci series

0 1 1 2 3 5 8 13 21 34

Program Menu
1. Generate default Fibonacci series
2. Generate custom Fibonacci series
Enter your choice : 2

Enter first term of Fibonacci series : 0
Enter second term of Fibonacci series : 1
Enter number of terms : 21

Generating required Fibonacci series

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765



2 Comments

  1. Abhishek Nandakumar

    April 23, 2007

    Post a Reply

    Disabled comments? Scared of critics visiting your blog?

    If I were a critic, I would have given that movie a D- without watching it. First impressions are usually last impressions, so I usually don’t go and watch the next movie if the previous one sucked.

    What in the world made you watch Bean of all movies?

  2. GQ

    July 1, 2007

    Post a Reply

    There was an error over here, about the number of terms of the Fibonacci series it was showing. Corrected to n-2 in loop to show terms.

    @Abhi: Wrong place to give the comment. Disable comments for Bean movie got selected by mistake.

Submit a Comment

Your email address will not be published. Required fields are marked *