The program below is intended to print the first 25 terms of the Fibonacci sequence with a recursive function. I got the output, but the problem is that the program is not halting and the values are going to negative values once they get to large.
#include <stdio.h>
int fibo(int, int, int);
int main()
{
int s, c, i;
s = 1;
c = 0;
i = 0;
fibo(s, c, i);
}
int fibo(int s, int c, int i)
{
for(i = 0; i <= 25; i++)
{
s = s + c;
c = s - c;
printf("%d\n", s);
fibo(s, c, i);
}
}
Expected output:
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
My output:
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
-1323752223
512559680
-811192543
-298632863
-1109825406
-1408458269
1776683621
368225352
2144908973
-1781832971
363076002
-1418756969
-1055680967
1820529360
764848393
-1709589543
-944741150
1640636603
695895453
-1958435240
-1262539787
1073992269
-188547518
885444751
...