Advertisement
Talilo

fibonacci.c

Apr 19th, 2023 (edited)
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. // ---------------------------------------------
  2. //Fonte:
  3. //https://www.onlinegdb.com/online_c_compiler
  4. //https://sites.icmc.usp.br/andretta/ensino/aulas/sme0330-1-15/fibonacci.c
  5.  
  6. // Programa, em linguagem C, que le um numero n
  7. // e imprime os n primeiros numeros da sequencia
  8. // de Fibonacci. Alem disso, aproxima a razao
  9. // aurea dividindo um termo pelo anterior.
  10. // ---------------------------------------------
  11.  
  12.  
  13. #include <stdio.h>
  14.  
  15. int main() {
  16.   long int a,b,c;
  17.   int n,cont;
  18.   double r;
  19.  
  20.   printf("Digite quantos termos da sequencia de Fibonacci voce quer:\n");
  21.   scanf("%d", &n);
  22.  
  23.   if (n <= 1) {
  24.     printf("Numero de termos invalido\n");
  25.   }
  26.   else {
  27.    
  28.     a = 1;
  29.     b = 1;
  30.     cont = 2;
  31.     printf("\n\n1\n1\n");
  32.  
  33.     while(cont < n) {
  34.       c = a + b;
  35.       r = c;
  36.       r = r/b;
  37.       printf("%ld %.15lf\n", c, r);
  38.       a = b;
  39.       b = c;
  40.       cont = cont + 1;
  41.       //eh a mesma coisa: cont++;
  42.     }
  43.   }
  44.  
  45.   return(0);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement