Fibonacci Series (Function/Recursive) in C Programming
Recursive Function
#include <stdio.h>
#include<conio.h>
int fibonacci(int);
void main()
{
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: ");
for (i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}
getch( );
}
int fibonacci(int n)
{
if (n <= 1)
{
return n;
}
else
{
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
In Function
#include <stdio.h>
#include<conio.h>
void fibonacci(int n);
void main()
{
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
fibonacci(n);
getch();
}
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
printf("Fibonacci series: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
}
Comments
Post a Comment