The fibonacci algorithm should look something like this:
#include <stdlib.h>
#include<stdio.h>
int fibo(int n)
{
int i,u1,u2,un;
u1=1;
u2=1;
un=1;
for i=3 : i<=n : i++;
{
un=u1+u2;
u1=u2;
u2=un;
}
return un;
}
int main()
{
int u;
u=fibo(4); /* example */
printf("The 4th term is %d\n", u);
return 0;
}
where n>0.
in thinbasic:
function fibo(byval n as integer) as integer
dim as integer i,u1,u2,un
u1=1
u2=1
un=1
for i=3 to n
un=u1+u2
u1=u2
u2=un
next
return un
end function
msgbox 0, fibo(4)
The second part of the task is more tricky and I can't think of a clean solution. You can drive the sequence till the integer types overflow and go negative but depending on your compiler, overflowing float types will trigger a run time error.
(For Longs I make the limit N=47)
Charles
Bookmarks