C O D E
CODE:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return (fibonacci(n – 1) + fibonacci(n – 2));
}
}
int main() {
int n;
printf(“Enter the number of terms you want in the Fibonacci series: “);
scanf(“%d”, &n);
printf(“Fibonacci Series: “);
for (int i = 0; i < n; i++) {
printf(“%d “, fibonacci(i));
}
return 0;
}
OUTPUT:
Enter the number of terms you want in the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
FOR COPY PASTE DOWNLOAD CODE FROM HERE:
Table of Contents
P R O G R A M M I N G
E X E C U T E
E X P L A N A T I O N
Understanding Series Generation in C Using Recursion
Introduction: It is a simple program that generates the Fibonacci series using recursion. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones. In this code, we have a recursive function fibonacci
that calculates the nth Fibonacci number, and a main
function to control the user interface and program flow.
fibonacci
that calculates the nth Fibonacci number, and a main
function to control the user interface and program flow.Code Explanation:
- Header Inclusion
#include <stdio.h>
This line includes the standard input/output library, which is necessary for using functions like
printf
andscanf
. - Recursive Fibonacci Function
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return (fibonacci(n - 1) + fibonacci(n - 2));
}
}
fibonacci
is a recursive function that calculates the nth Fibonacci number.- It takes an integer
n
as input and returns an integer as the result. - The base case of the recursion is when
n
is less than or equal to 1. In this case, it returnsn
(0 for n=0 and 1 for n=1), as the Fibonacci series starts with 0 and 1. - For
n
greater than 1, it recursively calls itself withn - 1
andn - 2
, sums the results of these recursive calls, and returns the sum. This is the essence of the Fibonacci series, where each number is the sum of the two preceding ones.
- Main Function
printf(“Enter the number of terms you want in the Fibonacci series: “);int main() {
int n;
scanf(“%d”, &n);
printf(“Fibonacci Series: “);
for (int i = 0; i < n; i++) {
printf(“%d “, fibonacci(i));
}
return 0;
}- The
main
function serves as the entry point of the program. - It declares an integer variable
n
to store the number of terms the user wants in the Fibonacci series. - It uses
printf
to display a message asking the user to enter the number of terms. - It uses
scanf
to read the user’s input and store it in the variablen
. - It prints the message “Fibonacci Series: ” to indicate that the following numbers are part of the Fibonacci series.
- It uses a
for
loop to generate and print the Fibonacci series up to the specified number of terms. Inside the loop, it calls thefibonacci
function for each term (from 0 to n-1) and prints the result.
- The
Execution Flow:
- The program starts with the
main
function. - The user is prompted to enter the number of terms they want in the Fibonacci series.
- The user inputs a value for
n
. - The program enters a loop that iterates from
i
= 0 toi
= n-1. - For each value of
i
, thefibonacci
function is called to calculate the ith term in the Fibonacci series. - The result is printed to the console, separated by spaces.
- The loop continues until
i
reaches n-1, generating the desired number of terms in the Fibonacci series. - The program terminates.
Key Concepts:
- Recursion: The
fibonacci
function uses recursion to calculate Fibonacci numbers. It breaks down the problem into smaller subproblems by calling itself with smaller inputs and combines the results to obtain the final answer. - Base Case: The base case of the recursion is when
n
is 0 or 1, where the function returns the corresponding value (0 or 1). Base cases are essential in recursive functions to prevent infinite recursion. - User Input: The
main
function handles user input and utilizes the value entered by the user to control the number of terms in the Fibonacci series. - Looping: A
for
loop is used to iterate through the values from 0 to n-1, and for each value ofi
, the corresponding Fibonacci number is calculated and printed. - Printing to Console: The
printf
function is used to display messages and print the Fibonacci series to the console.
In summary, the code provided demonstrates a simple implementation of generating the Fibonacci series in C using recursion. It uses a recursive function to compute the Fibonacci numbers, and the main
function handles user input and controls the program flow. This code is a fundamental example of recursive programming and is useful for educational purposes.
MORE PROGRAM :
- https://d-techsol.in/in-c-programming-write-a-code-of-lcm-of-two-number/
- https://d-techsol.in/write-a-program-of-addition-of-number-in-c/
- https://d-techsol.in/fibonacci-series-in-c-program-using-for-loop/
- https://d-techsol.in/easy-code-of-factorial-in-c-program/
- https://d-techsol.in/4-factorial-using-recursion-of-a-number-in-c-program/
BEST C PROGRAMMING BOOK :