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 do I Solve this by substitution?
How did the industrial Revolution impact the development of modern economic systems
write two sentences about pets use verb that tell what is happening now
Helppppp plssss!! Thanks
Express each ratio as a fraction in simplest form:8 dm to 3.2m
Most of the CO2 from cellular respiration is released during
Which structure is missing from the diagram, but helps mRNA and tRNA bind together? A. Ribosome B. DNA C. Mitochondrion D. Nucleus
Mark is having a party. He invited 35 people. Mark set up 8 tables with 4 chairs at each table. Dose Mark have enough tablels and chairs for all his guests?
(HELP MEH PLEASE) Write the equation of this line in slope-intercept form.
the width of a rectangle is 4 inches less than its length. The area of the rectangle is 45 square inches. What is the length of the rectangle? A. 4 inches B. 5