Program 49 - CSU1128 - CSE 2026 - Shoolini University

Program to generate fibonacci series till the number of terms entered by the user through function recursion

Explanation

The program generates the Fibonacci series up to the number of terms entered by the user using a recursive function.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Function Declaration

The program declares a recursive function fibonacci to generate the nth element in the Fibonacci series:

  • int fibonacci(int n): Declares the function with an integer parameter n.

Function Definition

The program defines the fibonacci function:

  • if (n == 0): Defines the base case where the first element in the series is 0.
  • return 0;: Returns 0 if the base case is met.
  • if (n == 1): Defines the base case where the second element in the series is 1.
  • return 1;: Returns 1 if the base case is met.
  • return fibonacci(n - 1) + fibonacci(n - 2);: Defines the recursive case where the nth element is equal to the sum of the previous two elements.

Variable Declaration

The main() function is defined, and a variable is declared:

  • int n: Variable to hold the number of elements in the series.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the number of elements in the series:

  • printf("Enter the number of elements in the series: ");: Prompts the user to enter the number of elements.
  • scanf("%d", &n);: Reads the number of elements and stores it in n.

Fibonacci Series Generation

The program generates the Fibonacci series up to the nth element using the fibonacci function:

  • printf("Fibonacci series: ");: Prints a label for the series.
  • for (int i = 0; i < n; i++): Loops through each element up to the nth element.
  • printf("%d ", fibonacci(i));: Calls the fibonacci function to generate the ith element and prints it.

Output Result

The program prints the Fibonacci series up to the nth element.

Program End

The program ends with printf("\n"); to print a new line and return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>

// Declare a recursive function to generate the nth element in the Fibonacci series
int fibonacci(int n)
{
    // Define the base case where the first two elements in the series are 0 and 1
    if (n == 0)
    {
        return 0;
    }
    if (n == 1)
    {
        return 1;
    }

    // Define the recursive case where the nth element is equal to the sum of the previous two elements
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
    printf("\n\n Program to generate fibonacci series till the number of terms entered by the user through function recursion. \n\n");

    // Declare a variable to hold the number of elements in the series
    int n;

    // Read the number of elements from the user
    printf("Enter the number of elements in the series: ");
    scanf("%d", &n);

    // Generate the Fibonacci series using the fibonacci function
    printf("Fibonacci series: ");
    for (int i = 0; i < n; i++)
    {
        printf("%d ", fibonacci(i));
    }
    printf("\n");

    return 0;
}


                

Output

Program to generate fibonacci series till the number of terms entered by the user through function recursion.

Enter the number of elements in the series: 10
Fibonacci series: 0 1 1 2 3 5 8 13 21 34