we were asked to write a program to generate a number of fibonacci as homework. so I wrote a program that generates the first n Fibonacci numbers. Here is my frist code that works fine.
# include <stdio.h> void main() { int a = -1, b = 1, c = 0, i, n, sum = 0 ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThefibonacci series is : \n\n") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d \t", c) ; b=c; a=b; } }
so I tried various combinations and I found out that my code would work well if I changed the 12th and 13th lines. i.e
# include <stdio.h> void main() { int a = -1, b = 1, c = 0, i, n, sum = 0 ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThefibonacci series is : \n\n") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d \t", c) ; a=b; b=c; } }
This is the same logical right. why does the first code give me the wrong output?
What are segmentation errors? (my compiler often tells me that there are segmentation errors in my code)
PS-i is a beggar. Only three weeks in c-language, and we learn about the cycles.
source share