0
I need to do a program that keeps the first 100 numbers of the Fibonacci sequence on file. I was able to make the sequence right, but at the time of the program save the file appears only the number "695895453".
How do I save all the numbers?
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main() {
setlocale (LC_ALL,"portuguese");
FILE * teste;
int a, b, aux, i;
a = 1;
b = 0;
for(i = 0; i < 100; i++)
{
aux = a + b;
a = b;
b = aux;
printf(" %d ,", aux);
}
if((teste = fopen("teste.txt","w")) == NULL)
{
printf("Erro de abertura! \n");
}
else
{
for(i = 0; i < 64; i++)
{
aux = a + b;
a = b;
b = aux;
fprintf(teste,"%d" ,aux);
fclose(teste);
}
return 0;
}
}
You first print the first 64 members of the Fibonacci sequence and then record the 64 members following sequence. If you want to save the first 64, reset the variables
aandb.– anonimo
It should actually be
a = 0;andb = 1;and these are the first two elements of the sequence and therefore should be recorded first and then recorded the next 62 elements.– anonimo