hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.

and than you

This is one way of going about it without much thinking

fn = [ Phi^ n - (-Phi)^-n ] / (2Phi-1)
for smaller values of n you need approximation.

Phi = ( 5^½ + 1 ) / 2 = 1.6180339 (the golden ratio)
^ = power

But this method is seldom used. This is more mathematical. Use Google as Dave suggested. You can get lots of material.

Simply put it this way, when ur applications demands a particular Fibonacci number (for eg. the 10th fibonacci number) then GOLDEN RATIO is the way to do it and if u require the whole series of numbers then the incremental method is the way.

G'day

where error?:sad:

1 error(s), 0 warning(s)

# include <iostream.h>
int fibonacci(int n)
{[INDENT] int x1 = 0, fib;
int x2 = 1;
if(n >= 1)
{[INDENT] for(int i=2;i<= n; i++)
{
[/INDENT][/INDENT][INDENT][INDENT][INDENT]fib = x1+ x2;
 x1 = x2;
x2 = fib;
[/INDENT][/INDENT][/INDENT][INDENT][INDENT] }

return fib;
[/INDENT][/INDENT][INDENT] }
[/INDENT]
#include<iostream>
using namespace std;
 
int fibonacci(int n)
{
int x1 = 0, fib;
int x2 = 1;
if(n >= 1)
{
for(int i=2;i<= n; i++)
{
fib = x1+ x2;
x1 = x2;
x2 = fib;
}}
 
return fib;
}
int main(){
for(int i=2;i<=10;i++) {
cout<<fibonacci(i)<<endl;
}
return 0;
}

this is ok now I'm finish it
but I need this


(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).

and

this is ok now I'm finish it
but I need this


(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).

(1) Run the program. Use a value bigger than 10 in the main loop.

(2) Surely you can make an attempt at these changes.

Please enclose posted code in code tags -- just like it says in the little box you reply in.

Here is the shortest code for finding the fibo series

#include <iostream>
using namespace std;

int main(){
    long a;
    cout << "Enetr Limit you want\n\n";
    cin >> a;
    cout << "Fibonachi numbers < " << a << endl;
    long f = 0, f2, f1 = 1;
    while(true) {
                f2 = f + f1;
                if(f2 > a) {
                      break;
                      }
             cout << "   " << f2 << endl;
             f = f1;
             f1 = f2;
             }
            system("pause");
           return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

>Here is the shortest code for finding the fibo series
I think this code is shorter, don't you?

void Fib(int fmax)
{
    int i = 1, f[2]  = { 0, 1 };
    while (f[i] < fmax) {
        cout << f[!i] << '\n';
        f[i^=1] = *f + f[1];
    }
}

;)

Here is the shortest code for finding the fibo series

I also found a short code (nearly exactly the same as yours):

#include <iostream>
using namespace std;

int main(void)
{
    int f0 = 0, f1 = 1, fn, repeat_times;
    cout << "Enter number of elements (the first two elements are always displayed): ";
    cin >> repeat_times;
    cout << endl << "Fibbonacci series: 0 1 ";
    for(int i = 0; i < repeat_times; i++)
    {
        fn = f0 + f1, f0 = f1, f1 = fn;
        cout << fn << " ";
    }
    cout << endl;
    return 0;
}

>Here is the shortest code for finding the fibo series
I think this code is shorter, don't you?

void Fib(int fmax)
{
    int i = 1, f[2]  = { 0, 1 };
    while (f[i] < fmax) {
        cout << f[!i] << '\n';
        f[i^=1] = *f + f[1];
    }
}

;)

The smallest and best optimized c++ code I ever saw for such a task !!!

A commen misunderstanding is that the closed form version,
is an approximation.

For integers, it will be just as accurate as the iterative or recursive version.

>For integers, it will be just as accurate as the iterative or recursive version.
Okay heres the bottom line:
If you want to calculate the nth Fibonacci number, use the closed form(i.e. the formula mentioned in Post #3).
If you want to print the series till some n, always use the iterative version suggested by ArkM and other.

It may look that the closed form is always better than the iterative form since it will be calculated faster, but it is not. The calculation of power takes more time than simply to add/subtract hence the best choice( if printing the whole series) is the iterative version.
Both are of O(n).
Recursive method in this case is evil. It is not suggested.

Now regarding the limits:
limit.h( newly called climits) header file contains the maximum value of int, float, double etc.
So, it is likely that you may run a loop till the current Fibonacci number is less than the INT_MAX (defined in climits).

The smallest and best optimized c++ code I ever saw for such a task !!!

but i copy paste it and encountered on error (unresolved external '_main')

im using borlan c++ may b thats y...........


but i tried making my own program for Fibonacci series and it worked
i m a beginner. i thought array will will best suite this problem.....
so here is my program and i think THIS ONE IS THE SHORTEST :cool: if not then its realllly realllly easy............
so feel free to replay and tell me how was it

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

void main(void)
{
int i,a[20];
a[0]=0;
a[1]=1;


for (i=0 ; i<=20 ; i++)
{
a[i+2]=a[i]+a[i+1];
}

for (i=0 ; i<=20 ; i++)
{
 cout<<a[i]<<"\n";

}
getch();
}

im using borland c++ 5.02 (old version ..ya but its gud :$)

Edited 5 Years Ago by Nick Evan: Added [CODE] tags

Fibonacci in its recursive flavor:

int fib(unsigned n) {
   if(n <= 1) return n;
   else return fib(n-1) + fib(n-2);
}

( returns the nth element of the Fibonacci series )

Or even shorter:

int fib(unsigned n) {
   return n <= 1 ? n : fib(n-1) + fib(n-2);
}

( returns the nth element of the Fibonacci series )

Edited 7 Years Ago by mvmalderen: tidy up some things

try out this code this is more easy then above

#include<iostream.h>
#include<conio.h>
void main()
{
   clrscr();
   int tf=0,i,s=1,n,f,fib=0;
   cout<<"Enter the total no of terms in series\n";
   cin>>n;
   for(i=0;i<n;++i)
   {
     fib=tf+s;
     cout<<tf<<" ";
     tf=s;
     s=fib;
   }
getch();
}

Edited 5 Years Ago by Nick Evan: n/a

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,fb,a=0,b=1;
cout<<"enter a number"; 
cin>>fb;
cout<<b;
for(i=1;i<fb;i++)
{
sum=a+b;
cout<<sum;
a=b;
b=sum;
}
getch();
}

hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.

and than you

............
soln is very simple , u jus hav 2 use a for loop n 3 variables as given below

#include<iostream.h>
#include<conio.h>
void main()
{
int a=-1,b=1,c=0,n;
cout<<"enter the numbers to be printed";
cin>>n;
for(int i=0;i<n;i++)
{
c=a+b;
cout<<c;
a=b;
b=c;
}
}

Edited 5 Years Ago by Nick Evan: Added [CODE] - tags

This article has been dead for over six months. Start a new discussion instead.