C PROGRAMMING PROGRAM • FUNCTIONS & RECURSION

Print Fibonacci Terms Using Recursion

Learn how to print fibonacci terms using recursion using a clear C program.

IntermediateRecursionFibonacciBase cases

PROBLEM UNDERSTANDING

Input and expected output

Sample input
8
Sample output
0 1 1 2 3 5 8 13

COMPLETE C PROGRAM

Complete C implementation

fibonacci-recursion.c
Open in compiler
#include <stdio.h>

unsigned long long fibonacci(int index)
{
    if (index <= 1) return (unsigned long long) index;
    return fibonacci(index - 1) + fibonacci(index - 2);
}

int main(void)
{
    int terms;

    printf("Enter number of terms from 1 to 40: ");
    scanf("%d", &terms);
    if (terms < 1 || terms > 40) return 1;
    for (int index = 0; index < terms; index++)
        printf("%llu%c", fibonacci(index), index == terms - 1 ? '\n' : ' ');
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

0 1 1 2 3 5 8 13
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Define each term as the sum of its two preceding recursive results.
  3. Display the computed result.

Define each term as the sum of its two preceding recursive results.

EFFICIENCY

Time and space complexity

Time complexity

O(2ⁿ)

Auxiliary space

O(n)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.