C PROGRAMMING PROGRAM • FUNCTIONS & RECURSION
Print Fibonacci Terms Using Recursion
Learn how to print fibonacci terms using recursion using a clear C program.
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
#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;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
0 1 1 2 3 5 8 13
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Define each term as the sum of its two preceding recursive results.
- 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.
