2
I’m doing a college exercise where the user must enter a number and the program must return the Fibonacci sequence. However, according to the teacher, it should start at 0 (ZERO), but my program starts at 1.
Note: The teacher has already corrected and will not accept any more modifications, I really want to understand why my program did not print the zero, once I declared a = 0
.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <locale.h>
main() {
setlocale(LC_ALL, "Portuguese");
int a, b, auxiliar, i, n;
a = 0;
b = 1;
printf("Digite um número: ");
scanf("%d", &n);
printf("\nSérie de Fibonacci:\n\n");
printf("%d\n", b);
for(i = 0; i < n; i++) {
auxiliar = a + b;
a = b;
b = auxiliar;
printf("%d\n", auxiliar);
}
}
From now on I thank you guys.
Because its first element in the sequence is
b
, naya
; seeprintf("%d\n", b)
– Woss
Man, what a ridiculous mistake... I thank you there brother. I will create an answer with the resolution. Hugs.
– tspereirarj
But do you want to change the program to start with Fib(0) ? To get 0, 1, 1, 2, 3, 5 ... ?
– Isac
No, @Isac, he’d want to do
printf("%d\n", a);
in place ofprintf("%d\n", b);
because he’s a funny person... [facepalm]– Marcelo Shiniti Uchimura