Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

5. Fibonacci series in C program using recursion easy method.

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:

DOWNLOAD FILE:

Table of Contents

P R O G R A M M I N G

Fibonacci series in c

E X E C U T E

fibonacci series in c using recursionn
Fibonacci series in c using recursionn

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.

Code Explanation:

  1. Header Inclusion
    #include <stdio.h>

    This line includes the standard input/output library, which is necessary for using functions like printf and scanf.

  2. 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 returns n (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 with n - 1 and n - 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.
  3. Main Function
    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;
    }
    • 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 variable n.
    • 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 the fibonacci function for each term (from 0 to n-1) and prints the result.

Execution Flow:

  1. The program starts with the main function.
  2. The user is prompted to enter the number of terms they want in the Fibonacci series.
  3. The user inputs a value for n.
  4. The program enters a loop that iterates from i = 0 to i = n-1.
  5. For each value of i, the fibonacci function is called to calculate the ith term in the Fibonacci series.
  6. The result is printed to the console, separated by spaces.
  7. The loop continues until i reaches n-1, generating the desired number of terms in the Fibonacci series.
  8. 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 of i, 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 :

BEST C PROGRAMMING BOOK :

Leave a Comment