Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

DATA STRUCTURES - LAB - R13 - 21

Week-1
Write recursive program which computes the nth Fibonacci number, for appropriate values of
n. Analyze behavior of the program Obtain the frequency count of the statement for various
values of n.
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, .. Begins with the terms 0 and 1
and has the property that each succeeding term is the sum of the two preceding
terms.

Algorithm steps:
1. Start.
2. Get the number n up to which Fibonacci series is generated.
3.Call to the function fib.
4.stop Algorithm fib
1.start
2.if n=0 or 1 then return n
3.else return fib(n-1)+fib(n-2)
4. Stop.

Program
/*
* C Program to find the nth number in Fibonacci series using recursion
*/
#include <stdio.h>
int fibo(int);
int main()
{
int num;
int result;

printf("Enter the nth number in fibonacci series: ");


scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
Int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}

OUTPUT
Enter the nth number in fibonacci series: 5
The 5 number in fibonacci series is 5
Enter the nth number in fibonacci series: 6
The 6 number in fibonacci series is 8

You might also like