Please what is the error with this my code... Fibonacci series | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please what is the error with this my code... Fibonacci series

Fibonaccis series showing runtime error. https://code.sololearn.com/c5hn1wnX3otP/?ref=app

30th Apr 2020, 8:17 PM
Umunnakwe Ephraim Ekene
Umunnakwe Ephraim Ekene - avatar
5 Answers
+ 3
So I modified it tho I don't give solutions but I kinda wanted to see if I can implement it correctly int main() { int num; int i; for(i=3;i>=-1;i--){ num=fibonacci_n(i); printf("%d\n",num); } } int fibonacci_n(int n) { int m=1; int count; for(count=0;count<=n;count++) { m+=count; } return m; }
30th Apr 2020, 9:33 PM
Abhay
Abhay - avatar
+ 3
for(count=1;count<=n;count++) { n+=count; } this is running for infinite number of times. because count would never be greater than n (which means count<=n is always true) since with every iteration you are adding n+=count(n=n+count) so a solution would be to use a new variable for n
30th Apr 2020, 8:51 PM
Rohit
+ 2
I dunno C so I am not able to solve it but you need to read your program to see what you are doing ,your program is running endlessly with no way to stop that function with n-1 and also you are adding count to n while keeping count<n ,really?
30th Apr 2020, 8:40 PM
Abhay
Abhay - avatar
0
Thanks alot.
1st May 2020, 6:28 PM
Umunnakwe Ephraim Ekene
Umunnakwe Ephraim Ekene - avatar
0
I have edited the code. If only i could mark everyones suggestion๐Ÿ™Œ๐Ÿ™Œ๐Ÿ™Œ๐Ÿ™Œ๐Ÿ™Œ
1st May 2020, 6:29 PM
Umunnakwe Ephraim Ekene
Umunnakwe Ephraim Ekene - avatar