C Examples Fibonacci Series
## C Programming: Generating the Fibonacci Series
The Fibonacci series is a sequence of numbers where each number (after the first two) is the sum of the two preceding ones.
The sequence typically starts as follows:
$$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, \dots$$
Mathematically, it is defined by the recurrence relation:
* $F(0) = 0$
* $F(1) = 1$
* $F(n) = F(n-1) + F(n-2)$ for $n \ge 2$
In this tutorial, you will learn how to implement the Fibonacci series in C using two different approaches:
1. Generating a **fixed number of terms** in the sequence.
2. Generating terms **up to a specified maximum value**.
---
## Example 1: Generate a Fixed Number of Fibonacci Terms
This program prompts the user to enter the number of terms they want to generate, then uses a `for` loop to calculate and print each term sequentially.
### C Source Code
```c
#include
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("\n");
return 0;
}
```
### Output
```text
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
```
### How It Works
1. **Initialization**: We initialize the first two terms, `t1 = 0` and `t2 = 1`.
2. **Looping**: The `for` loop runs $n$ times.
3. **Printing and Updating**:
* In each iteration, we print the current term `t1`.
* We calculate the next term: `nextTerm = t1 + t2`.
* We shift our window forward: `t1` becomes `t2`, and `t2` becomes `nextTerm`.
---
## Example 2: Generate Fibonacci Series Up to a Specified Value
This program generates Fibonacci numbers dynamically and stops as soon as the next term exceeds a user-defined maximum limit.
### C Source Code
```c
#include
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive integer limit: ");
scanf("%d", &n);
// Display the first two terms
printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
// Generate terms as long as they are less than or equal to the limit
while(nextTerm <= n)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
printf("\n");
return 0;
}
```
### Output
```text
Enter a positive integer limit: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
```
### How It Works
1. **Initial Output**: The program prints the first two terms (`0` and `1`) manually.
2. **Condition Check**: The `while` loop evaluates whether `nextTerm` is less than or equal to the user's limit `n`.
3. **Updating**: Inside the loop, the variables are updated sequentially to compute the next Fibonacci number. The loop terminates when `nextTerm` exceeds `n`.
---
## Important Considerations
### 1. Integer Overflow
The Fibonacci sequence grows exponentially. In C, standard 32-bit signed integers (`int`) can only store values up to $2,147,483,647$ (the 47th Fibonacci term).
* If you try to generate terms beyond this limit, the values will overflow, resulting in negative numbers or incorrect outputs.
* **Solution**: For larger sequences, use `long long` (which supports up to $9,223,372,036,854,775,807$) and the `%lld` format specifier.
### 2. Alternative Implementations
While the iterative approach shown above is highly efficient with $O(n)$ time complexity and $O(1)$ space complexity, the Fibonacci series can also be solved using:
* **Recursion**: Simple to write but highly inefficient ($O(2^n)$ time complexity) without memoization.
* **Dynamic Programming**: Stores previously computed values in an array to avoid redundant calculations.
YouTip