Introduction
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. This guide will show you how to write a C program to print the Fibonacci series using recursion.
Fibonacci Series Definition:
- ( F(0) = 0 )
- ( F(1) = 1 )
- ( F(n) = F(n-1) + F(n-2) ) for ( n > 1 )
Example:
- Input: ( n = 5 )
- Output: ( 0, 1, 1, 2, 3 )
Problem Statement
Create a C program that:
- Takes the number of terms to be printed in the Fibonacci series as input from the user.
- Uses a recursive function to generate each Fibonacci number.
- Prints the Fibonacci series up to the specified number of terms.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
for standard input-output functions. - Write the Recursive Function: Define a recursive function that calculates the nth Fibonacci number.
- Write the Main Function: Define the
main
function to take user input and print the Fibonacci series. - Input the Number of Terms: Use
scanf
to take input from the user for the number of terms. - Print the Fibonacci Series: Use a loop in the
main
function to call the recursive function and print each Fibonacci number. - Display the Result: Use
printf
to display the Fibonacci series.
C Program to Print the Fibonacci Series Using Recursion
#include <stdio.h> // Step 2: Define the recursive function to calculate Fibonacci numbers int fibonacci(int n) { if (n == 0) { return 0; // Base case: F(0) = 0 } else if (n == 1) { return 1; // Base case: F(1) = 1 } else { return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case: F(n) = F(n-1) + F(n-2) } } int main() { // Step 3: Declare variables to hold the number of terms int n; // Step 4: Prompt the user to enter the number of terms printf("Enter the number of terms: "); scanf("%d", &n); // Step 5: Print the Fibonacci series printf("Fibonacci series: "); for (int i = 0; i < n; i++) { printf("%d ", fibonacci(i)); } printf("\n"); return 0; // Return 0 to indicate successful execution }
Explanation
Step 2: Define the Recursive Function
- The
fibonacci
function takes an integern
as input. - Base Cases:
- If
n
is 0, the function returns 0 (since ( F(0) = 0 )). - If
n
is 1, the function returns 1 (since ( F(1) = 1 )).
- If
- Recursive Case:
- If
n
is greater than 1, the function returns the sum offibonacci(n - 1)
andfibonacci(n - 2)
.
- If
Step 3: Declare Variables
- The variable
n
stores the number of terms in the Fibonacci series that the user wants to print.
Step 4: Input the Number of Terms
- The program prompts the user to enter the number of terms using
scanf
.
Step 5: Print the Fibonacci Series
- The program uses a
for
loop to iterate from 0 ton-1
:- For each iteration, it calls the
fibonacci
function with the current index and prints the returned value.
- For each iteration, it calls the
Step 6: Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example 1:
Enter the number of terms: 5 Fibonacci series: 0 1 1 2 3
Example 2:
Enter the number of terms: 8 Fibonacci series: 0 1 1 2 3 5 8 13
Conclusion
This C program demonstrates how to generate the Fibonacci series using a recursive function. It covers basic concepts such as recursion, base cases, and user input, making it a useful example for beginners learning C programming.