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

Recursive Function For Fibonacci Series

The document describes a C program that prints the Fibonacci series using a recursive function. It includes code for the function and main program, along with instructions for running the program in Dev C++. The Fibonacci series is defined, and the document explains the recursive approach to calculating the series terms.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Recursive Function For Fibonacci Series

The document describes a C program that prints the Fibonacci series using a recursive function. It includes code for the function and main program, along with instructions for running the program in Dev C++. The Fibonacci series is defined, and the document explains the recursive approach to calculating the series terms.

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

You're working with a C program that prints the Fibonacci series using a recursive function.
This is a classic programming concept, and it's very useful to understand both recursion and
how functions work.
✅ 1. What is This Program All About?
This program:

●​ Asks the user to enter a number n


●​ Then, it prints the first n numbers in the Fibonacci series
●​ It uses a recursive function called fibonacci() to calculate each term

📌 What is the Fibonacci Series?


It’s a sequence of numbers like this:

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

Each number is the sum of the previous two numbers:

F(0) = 0
F(1) = 1
F(2) = F(1) + F(0) = 1
F(3) = F(2) + F(1) = 2
F(4) = F(3) + F(2) = 3
...

🧠 Recursive Function Used:


long fibonacci(long n) {
if (n == 0 || n == 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

This means:

●​ If n is 0 or 1, return n
●​ Otherwise, call the function again with n-1 and n-2, and add them
✅ 2. How to Run This in Dev C++
🛠 Step-by-Step Instructions:
🔹 Step 1: Open Dev C++
●​ Click the Dev C++ shortcut
●​ Go to File → New → Source File

🔹 Step 2: Paste and Modify Code


Paste the code below (with slight corrections for Dev C++ compatibility):

#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"); // use this instead of getch()
return 0;
}

long fibonacci(long n) {
if (n == 0 || n == 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
✅ Why the changes?
●​ #include <conio.h> and getch() cause errors on modern compilers
●​ system("pause"); keeps the window open in Windows
●​ Use %ld for long in scanf and printf

🔹 Step 3: Save the File


●​ Go to File → Save As
●​ Save it as fibonacci.c
●​ Set “Save as type” to C Source Files

🔹 Step 4: Compile and Run


●​ Click Execute → Compile & Run or press F11
●​ Enter a number (e.g., 7)
●​ You’ll see output like:

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

🧠 How to Explain the Program in Class


You can say:

"This program prints the first n Fibonacci numbers using a recursive function.​
The function fibonacci(n) calls itself with n-1 and n-2 to calculate the value.​
The base case is when n is 0 or 1 — it just returns n.​
It shows how recursion works by solving a big problem in smaller parts."

✅ Summary Table
Step Action

1 Open Dev C++


2 Paste corrected code

3 Save file as .c

4 Press F11 to Compile and Run

5 Enter an integer input

6 View and explain output

You might also like