Are you familiar with the Fibonacci sequence? It has a rich history spanning nearly a millennium! Originating from the renowned Italian mathematician Leonardo of Pisa, commonly known as Fibonacci, in 1202, this intriguing numerical pattern has intrigued mathematicians, scientists, and artists for generations. In this article, we'll delve into the fundamentals of the Fibonacci sequence in C and demonstrate how it can showcase your proficiency in coding during interviews. Let's embark on a journey to uncover the secrets of this age-old sequence.
Getting Started
What is the Fibonacci Sequence?
The Fibonacci sequence is a series of numbers that starts with 0 and 1. Each subsequent number is derived by adding the two preceding numbers, creating a pattern where each number equals the sum of its two predecessors. This results in an ever-growing sequence. The sequence follows the pattern 0, 1, 1, 2, 3, 5, 8, 13, 21, and so forth, with its expansion continuing indefinitely. This progression can be illustrated as follows: 0 + 1 = 1; 1 + 1 = 2; 1 + 2 = 3; 2 + 3 = 5; 5 + 8 = 13, and so on.
Algorithm of Fibonacci Series in programming language C
To develop an algorithm for generating a Fibonacci series in the C programming language, the following steps should be followed:
Step 1: Declare three variables A, B, and C of type int (Integer). Step 2: Assign initial values to A = 0 and B = 1. Step 3: Display the values of A and B. Step 4: Calculate C as the sum of A and B. Step 5: Display the value of C. Step 6: Update the value of A to the current value of B, and update the value of B to the current value of C. Step 7: Repeat steps 4 through 6 for 'n-2' times to generate a series with 'n' elements.
Writing a Program to Find Fibonacci Numbers
There are various approaches to creating a program for finding Fibonacci numbers:
1. C Program for Non-Recursive Fibonacci Series
A non-recursive approach employs loops to compute the sequence without invoking itself. Below is a C program that computes the Fibonacci series without recursion:
#include <stdio.h>
int main() {
float firstValue = 1.7f, secondValue = 2.6f, newValue, numberOfTerms;
printf("Enter the number of terms: ");
scanf("%f", &numberOfTerms);
printf("\n%.2f %.2f", firstValue, secondValue); // Printing the first two values
for (int i = 2; i < (int)numberOfTerms; ++i) { // Loop starts from 2 as the first two values are already printed
newValue = firstValue + secondValue;
printf(" %.2f", newValue);
firstValue = secondValue;
secondValue = newValue;
}
return 0;
}
Output
Enter the number of terms: 5
1.70 2.60 4.30 7.90 12.20
2.Generating Fibonacci Series with Recursion in C
A recursive function is one that invokes itself. For instance, to compute the 5th number in the sequence, a recursive function would call itself to calculate the 3rd and 4th numbers, and then sum them together. Below is a C program that computes the Fibonacci series using recursion:
#include <stdio.h>
void printCustomFibonacci(int count) {
static float firstValue = 1.3f, secondValue = 3.0f, newValue;
if (count > 0) {
newValue = firstValue + secondValue;
firstValue = secondValue;
secondValue = newValue;
printf("%.2f ", newValue);
printCustomFibonacci(count - 1);
}
}
int main() {
int numberOfElements;
printf("Enter the number of elements: ");
scanf("%d", &numberOfElements);
printf("Fibonacci Series: ");
printf("%.2f %.2f ", 1.3f, 3.0f);
printCustomFibonacci(numberOfElements - 2); // n-2 because 2 numbers are already printed
return 0;
}
Output
Enter the number of elements: 6
Fibonacci Series: 1.30 3.00 4.30 7.30 11.60 19.90
3.Generating Fibonacci Series in C Using Functions
In this C program, a loop is employed to iterate and display consecutive elements in a Fibonacci series up to 'n' terms. F1 and F2 are treated individually, while two dedicated variables store each preceding element, ensuring they are updated for computing the next term. Here's an example:
// C Program to print the Fibonacci series using iteration (loops)
#include <stdio.h>
void printCustomFibonacci(int count) {
if (count < 1) {
printf("Invalid number of terms\n");
return;
}
// Initialize first two terms
float firstValue = 2.3f;
float secondValue = 3.0f;
// Print the first two terms
printf("%.2f %.2f ", firstValue, secondValue);
// For loop to print Fibonacci series
for (int i = 3; i <= count; i++) {
// Calculate and print the next term
float newValue = firstValue + secondValue;
printf("%.2f ", newValue);
firstValue = secondValue;
secondValue = newValue;
}
}
int main() {
int numberOfTerms = 9;
printCustomFibonacci(numberOfTerms);
return 0;
}
Output
2.30 3.00 5.30 8.30 13.30 21.60 34.90 56.50 91.40
4. Generating Fibonacci Sequence Until a Specified Number
In this program, we utilize a while loop to display all Fibonacci numbers up to (but less than) 'n'.
#include <stdio.h>
int main() {
float firstValue = 1.7f, secondValue = 2.0f, nextTerm;
int userInput;
printf("Enter a positive number: ");
scanf("%d", &userInput);
// Display the first two terms which are specified
printf("Fibonacci Series: %.2f, %.2f, ", firstValue, secondValue);
nextTerm = firstValue + secondValue;
// Loop to generate and print Fibonacci series until nextTerm is greater than userInput
while (nextTerm < userInput) {
printf("%.2f, ", nextTerm);
firstValue = secondValue;
secondValue = nextTerm;
nextTerm = firstValue + secondValue;
}
return 0;
}
Output
Enter a positive number: 10
Fibonacci Series: 1.70, 2.00, 3.70, 5.70,
Take a look at the table below to observe the step-by-step progression of the Fibonacci sequence based on the provided C program:
| t1 | t2 | nextTerm | Condition |
|---|---|---|---|
| 0 | 1 | 1 | True (nextTerm <= n). Print 1. |
| 1 | 1 | 2 | True (nextTerm <= n). Print 2. |
| 1 | 2 | 3 | True (nextTerm <= n). Print 3. |
| ... | ... | ... | ... |
| 34 | 55 | 89 | True (nextTerm <= n). Print 89. |
| 55 | 89 | 144 | False (nextTerm > n). End loop. |
Summary
Non-Recursive Fibonacci Series: This approach generates Fibonacci numbers using loops, avoiding recursion. It initializes the first two terms and iterates through a loop to calculate subsequent terms.
Fibonacci Series Using Recursion: This method computes Fibonacci numbers using recursive function calls. It defines a function that calls itself to calculate Fibonacci numbers, simplifying the code but potentially leading to stack overflow for large inputs.
Fibonacci Sequence Up to a Certain Number: This approach generates Fibonacci numbers up to a specified value using a loop. It initializes the first two terms and iterates through a loop until reaching the specified limit.
Fibonacci Sequence Using Function: This method employs a function and a loop to generate Fibonacci numbers. It initializes the first two terms and iterates through a loop to calculate subsequent terms, utilizing a function for modular code.
Table Illustrating Fibonacci Sequence Progression: The provided table visually represents the step-by-step progression of the Fibonacci sequence. It displays the values of the terms involved in each iteration and the associated conditions, aiding in understanding the sequence's generation process.
