kyduyenhckd226 kyduyenhckd226
  • 14-10-2021
  • Computers and Technology
contestada

Using language c, find the nth Fibonacci, knowing that nth Fibonacci is calculated by the following formula: - If n = 1 Or n = 2 then F(n) = 1 - If n>2 then F(n) = F(n-1) + F(n-2)

Respuesta :

tonb
tonb tonb
  • 14-10-2021

Answer:

#include <stdio.h>

int fib(int n) {

 if (n <= 0) {

   return 0;

 }

 if (n <= 2) {

   return 1;

 }

 return fib(n-1) + fib(n-2);

}

int main(void) {

 for(int nr=0; nr<=20; nr++)

   printf("Fibonacci %d is %d\n", nr, fib(nr) );

 return 0;

}

Explanation:

The code is a literal translation of the definition using a recursive function.

The recursive function is not per se a very efficient one.

Answer Link

Otras preguntas

How did the cotton gin lead to the spread of slavery? (Hint: There are three reasons)
Which best describes the role of viceroys in Spain's American empire? A. Viceroys watched over the rights of the king's Indian subjects. B. Viceroys made sur
SHOPPING you went to the mall with $80. You spent 25% of your money on a pair of shorts and 65% of the remainder on sandals. How much did you spent on the sanda
How is infinite .99999999999 equal 1? (explain)
What is the degree of comparison of the underlined adverb in the sentence? Alyssa smiled at the audience as she danced gracefully across the stage. A. compara
21 is 70 % of what number?
Will the new cells have the same genetic material as the parent cell or will each ceell have different genetic matireal?
The mestizo resulted from?
what is utilitarianism?
Which action represents a break with George Washington''s policies? An amendment limiting the president to two terms in office Close diplomatic ties with Great