Friday, March 4, 2011

User-Defined Function

The Fibonacci numbers are defined recursively as follows:
F1 = 1
F2 = 1
Fn = F n - 1 + F n - 2, n > 2
Write a function that will generate and print the first n Fibonacci numbers. Test the function for n = 5, 10 , and 15.

#include <stdio.h>
void fibonacci(int n);
void main()
{
int n;
clrscr();
printf("Enter the value of Term:");
scanf("%d",&n);
fibonacci(n);
getch();
}
void fibonacci(int n)
{
int total[20],a=1,b=1,i=0;
do
{
total[i]=a+b;
a=b;
b=total[i];
}
while(++i<=n);
for(i=i-2;i>=0;--i)
printf(" %d",total[i]);
}

OUTPUT
Enter the value of Term : 5
13  8  5  3  2

Enter the value of Term : 10
144  89  55  34  21  13  8  5  3  2

Enter the value of Term : 15
1597  987  610  377  233  144  89  55  31  21  13  8  5  3  2

No comments:

Post a Comment