Fibonacci series
FIBONACCI SERIES:
The Fibonacci Series is an infinite sequence of integers.
like;
0,
1, 1, 2, 3, 5, 8, 13 and so on........
The first two
integers 0 and 1 are initial values. The Nth Fibonacci Number where N
>2 is the sum of two immediate preceding two elements. The recursive
definition to find nth Fibonacci number is given by,
The C function to find the nth
Fibonacci number using recursive definition is shown in example 1.
The way to find fib(4) is shown in figure 1. The thick arrows represent either a call to function fib() or to the value. The dashed arrows point to the result after computation. The iterative technique for this much more efficient than the recursive technique.
C PROGRAM TO DISPLAY FIBONACCI SERIES
/* Fibonacci series program in C language */
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are:\n", n);
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}
return 0;
}
Output:
Enter the number of elements:15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Comments
Post a Comment