Given an integer K, find the KthFibonacci number using recursion.

Write a function that accepts an integer K. The function should return Kth Fibonacci number using recursion.

Input:

10

where:

First line represents a value of K
Output:
55

Respuesta :

C program that find Fibonacci number using recursion

#include<stdio.h>

int fib(int k)  /*Function that returns Fibonacci number using recursion*/

{  

  if (k <= 1)  

     return k;  

  return fib(k-1) + fib(k-2); //Recursive loop

}  

  int main ()  //driver function

{  

 int k;

 printf("Enter the Number for which we requires Fibonacci number-\n"); /*Taking input as k integer for finding its Fibonacci number*/

 scanf("%d",&k);

 printf("%d", fib(k));  //Calling function fib

  return 0;  

}

Output

Enter the Number for which we requires Fibonacci number  -10

55

Function that returns Fibonacci number using recursion

int fib(int k)  

{  

  if (k <= 1)  

     return k;  //returning Fibonacci Number

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

}  

Here fib is the function of return type integer which accept a parameter k of integer type. In this function we are returning Fibonacci number.