2.2.2 Recursive Function for Fibonacci Serie
2.2.2 Recursive Function for Fibonacci Serie
long fibonacci(long n)
{
if (n == 0 || n == 1) //Base case
return n;
else
return (fibonacci(n-1) + fibonacci(n-2)); //recursive case
}
Type the following main program together with the recursive function to test it.
#include<stdio.h>
#include<conio.h>
int main()
{
long n;
getch();
return 0;
}
Guide to help you carry out this Recursive Fibonacci Function Practical using Visual Studio,
and how to explain it confidently to your lecturers.
0, 1, 1, 2, 3, 5, 8, 13, ...
#include <stdio.h>
#include <conio.h>
int main() {
long n;
getch();
return 0;
}
long fibonacci(long n) {
if (n == 0 || n == 1) // Base case
return n;
else
return (fibonacci(n - 1) + fibonacci(n - 2)); // Recursive case
}
○ Replace the default main.c or Program.c code with the code above.
3. Fix any issues
#include <stdio.h>
#include <stdlib.h>
int main() {
long n;
printf("\n");
system("pause"); // optional, keeps the window open
return 0;
}
long fibonacci(long n) {
if (n == 0 || n == 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
“This program prints the first n terms of the Fibonacci series using a recursive function.
The function fibonacci() is defined such that:
📤 Output:
Fibonacci series terms are: 0 1 1 2 3 5 8
Use the textbook code Paste and fix any errors if needed