Write a C program to find the Fibonacci sequence of n terms using recursion.
Understand the Problem
The question is asking for a C program that calculates the Fibonacci sequence for a given number of terms using recursion. This involves determining how to implement the Fibonacci sequence in code and ensure that it works appropriately for any positive integer input.
Answer
The complete C program for calculating the Fibonacci sequence using recursion is provided above.
Answer for screen readers
The complete C program to calculate the Fibonacci sequence using recursion is:
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Steps to Solve
- Define the Fibonacci Function Define the recursive function for calculating Fibonacci numbers. If the input ( n ) is 0, return 0; if ( n ) is 1, return 1. For other values, call the function recursively.
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
- Input Handling Ask the user for the number of terms they want in the Fibonacci sequence. Store this value in a variable.
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
-
Display the Fibonacci Sequence
Use a loop to print the Fibonacci numbers for the requested number of terms, calling the
fibonacci
function for each index.
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
- Complete Code Combine all parts to create a complete C program.
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
The complete C program to calculate the Fibonacci sequence using recursion is:
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
More Information
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. This is a common example used to demonstrate recursion in programming.
Tips
- Forgetting to include the base cases in the recursive function, which can lead to infinite recursion.
- Not handling negative inputs properly. The program assumes the input is a non-negative integer.
- Miscounting terms during the loop, which could lead to one fewer or one more term than desired.
AI-generated content may contain errors. Please verify critical information