0% found this document useful (0 votes)
3 views5 pages

2.2.2 Recursive Function for Fibonacci Serie

The document provides a guide for implementing and testing a recursive Fibonacci function in C using Visual Studio. It includes the full code, step-by-step instructions for running the program, and tips on explaining the concept of recursion. Additionally, it emphasizes the importance of practicing the code and understanding the underlying principles before presenting it to an audience.

Uploaded by

plannyhub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

2.2.2 Recursive Function for Fibonacci Serie

The document provides a guide for implementing and testing a recursive Fibonacci function in C using Visual Studio. It includes the full code, step-by-step instructions for running the program, and tips on explaining the concept of recursion. Additionally, it emphasizes the importance of practicing the code and understanding the underlying principles before presenting it to an audience.

Uploaded by

plannyhub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

2.2.

2 Recursive Function for Fibonacci Series

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>

long fibonacci(long); //function signature

int main()
{
long n;

printf("Enter an integer: ");


scanf("%d", &n);

printf("Fibonacci series terms are: ");


for (int i = 0; i < n; i++)
{
printf("%d ", fibonacci(i)); //function call
}

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.

1. What is This Practical About?


You are testing a recursive function that prints the Fibonacci series — a sequence of
numbers like this:

0, 1, 1, 2, 3, 5, 8, 13, ...

Each number is the sum of the previous two numbers.


You're using recursion, which means the function calls itself to calculate the next number.

📋 2. Code to Use in Visual Studio


Here’s the full code from your textbook:

#include <stdio.h>
#include <conio.h>

long fibonacci(long); // function signature

int main() {
long n;

printf("Enter an integer: ");


scanf("%d", &n);

printf("Fibonacci series terms are: ");


for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i)); // function call
}

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
}

3. How to Run This in Visual Studio


Step-by-Step:
1. Open Visual Studio

○ Click Create a new project


○ Select Console App (for C/C++)
○ Name it RecursiveFibonacci
2. Paste the Code

○ Replace the default main.c or Program.c code with the code above.
3. Fix any issues

○ If you get an error with getch() or <conio.h>, you can:


■ Replace getch(); with system("pause");
■ Or remove it altogether if it causes issues.
■ Also replace %d with %ld since n is a long.
4. Final clean version (portable):

#include <stdio.h>
#include <stdlib.h>

long fibonacci(long); // function signature

int main() {
long n;

printf("Enter an integer: ");


scanf("%ld", &n);

printf("Fibonacci series terms are: ");


for (int i = 0; i < n; i++) {
printf("%ld ", fibonacci(i)); // function call
}

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);
}

4. How to Explain This in Front of Your Lecturer


Here’s how you can explain it:

“This program prints the first n terms of the Fibonacci series using a recursive function.
The function fibonacci() is defined such that:

● If n is 0 or 1 (the base case), it returns n.


● Otherwise, it calls itself twice to get fibonacci(n-1) and fibonacci(n-2) and adds
them.
This technique is called recursion, where a function solves a big problem by breaking it
into smaller versions of itself.”

🔍 5. Sample Input and Output


Input:
Enter an integer: 7

📤 Output:
Fibonacci series terms are: 0 1 1 2 3 5 8

🎯 6. What to Practice Before Class


● Practice running the code at least twice in Visual Studio.
● Try different input values (5, 10, 15).
● Try saying the explanation out loud like you're presenting.
Summary of What to Do
Task Action

Set up Visual Studio Create new Console App Project

Use the textbook code Paste and fix any errors if needed

Test before class Try several inputs and check outputs

Understand recursion Know that the function calls itself

Explain clearly Talk about base case, recursive case, and


output

You might also like