Thread: Fibonacci using do-while

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    1

    Question Fibonacci using do-while

    Hello!

    I have to write some functions using different structures to determine the result of the Fibonacci sequence from a given number and all below. For example, if I want the result of the sixth therm of Fibonacci sequence, I have to print on screen the following text:

    0 - F(0) = 0
    1 - F(1) = 1
    2 - F(2) = 1
    3 - F(3) = 2
    4 - F(4) = 3
    5 - F(5) = 5
    6 - F(6) = 8

    I have already done using a recursive function, but I don'y know how to do using the do-while loop. Thank you.

    ----------------------------------------------------------------------------------------------------------
    Code:
    /* aula0301.h */
    
    #ifndef _AULA_0301_
    #define _AULA_0301_ "@(#)aula0301.h $Revision: 1.1 $"
    
    
    typedef unsigned long long ull;
    typedef unsigned short byte;
    
    
    ull
    CalcularTermoSerieFibonacci(byte);
    
    
    #endif
    ----------------------------------------------------------------------------------------------------------
    Code:
    /* aula0301a.c */
    
    #include "aula0301.h"
    
    
    ull
    CalcularTermoSerieFibonacci(byte numero)
    {
      if (numero == 0)
        return 0;
      if (numero == 1 || numero == 2)
        return 1;
      return (CalcularTermoSerieFibonacci(numero - 1) +             CalcularTermoSerieFibonacci(numero - 2));
    }
    ----------------------------------------------------------------------------------------------------------
    Code:
    /*aula0301b.c */
    
    #include "aula0301.h"
    
    ull
    CalcularTermoSerieFibonacci(byte numero)
    {
      
      ull contador = 2;
      
      do
      {
        contador = (contador - 1) + (contador - 2);  
      }
      while (contador != numero);
      
      return contador;
    }
    ---------------------------------------------------------------------------------------------------------
    Code:
    /* aula0302.c */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "aula0301.h"
    
    
    #define NUMERO_ARGUMENTOS                       2 
    #define OK                                      0
    #define NUMERO_ARGUMENTOS_INVALIDO              1
    #define ARGUMENTO_INVALIDO                      2
    #define EOS                                     '\0' 
    
    
    int main(int argc, char *argv[])
    {
    
    
      unsigned short indice;
    
    
      if (argc != NUMERO_ARGUMENTOS)
      {
        printf("Uso: %s <numero-inteiro-nao-negativo>\n", argv[0]);
        exit(NUMERO_ARGUMENTOS_INVALIDO);
      }
    
    
      printf("\n");
    
    
      for (indice = 0; indice <= atoi(argv[1]); indice++)
      {
        printf("F(%hu) = %llu\n", indice,
                CalcularTermoSerieFibonacci((byte) indice));
      }
    
    
      printf("\n");
    
    
      return OK;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You need two variables like

    int F_2 = 0;
    int F_1 = 1;

    Then you can do
    int F = F_2 + F_1;

    You then roll the result as
    F_2 = F_1;
    F_1 = F;

    I can't say much more without just giving you the answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Fibonacci
    By Thedog in forum C Programming
    Replies: 3
    Last Post: 01-17-2011, 06:04 PM
  2. Fibonacci
    By Paged in forum C Programming
    Replies: 2
    Last Post: 12-22-2010, 04:42 PM
  3. Fibonacci
    By saria777 in forum C Programming
    Replies: 25
    Last Post: 06-16-2010, 07:26 AM
  4. sum of fibonacci
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 09:28 AM
  5. fibonacci(10,000)
    By blindman858 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2005, 12:03 AM

Tags for this Thread