Fibonacci Program in C
The Fibonacci Series is a sequence of numbers where each number is a sum of the previous two numbers. This article will explore how to implement the Fibonacci Series in the C programming language using different methods.
The Fibonacci Series is a fascinating sequence that appears in many areas of mathematics and science. The application of the Fibonacci Series can be seen in nature (growing or splitting of trees), Math (golden ratio, Pascals Triangle), Poetry, and Art. So, in this article, we will explore how to create a Fibonacci Series program in C or find any particular number in the given series.
Must Check: What is C Programming?
Must Check: Top Online C Programming Courses and Certifications
So, let’s start with the formal definition of the Fibonacci Series.
What is Fibonacci Series?
Fibonacci numbers are the sequence of numbers defined by the linear equation:
Fn = Fn-1 + Fn-2, with
F0 = 0, F1 = F2 = 1, and n = 2, 3, ….
In simple terms, Fibonacci numbers are a sequence of numbers in which each number is a sum of the previous two.
Example
n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Fn | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 |
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
How to Implement Fibonacci Series in C?
Let’s start with a simple Fibonacci Series in C.
Problem Statement: Generate a Fibonacci Series up to a certain number of terms (i.e., user input).
#include <stdio.h>
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;
if(i < n) { printf(", "); } }
return 0;}
Output
Enter the number of terms: 5 Fibonacci Series: 0, 1, 1, 2, 3
Fibonacci Series using Recursion in C
Recursive functions break down a problem into smaller problems and use themselves to solve them. Now, let’s see how to use a recursive function to generate the Fibonacci series.
Problem Statement: Generate the first 15 Fibonacci numbers using a recursive function.
#include <stdio.h>
int fibonacci(int n, int* memo) { if (n <= 1) return n;
if (memo[n] != -1) return memo[n];
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo); return memo[n];}
int main() { int n, i;
printf("Enter the number of terms: "); scanf("%d", &n);
int memo[n+1]; for (i = 0; i <= n; i++) { memo[i] = -1; }
printf("Fibonacci Series: "); for (i = 0; i < n; i++) { printf("%d", fibonacci(i, memo)); if (i < n - 1) { printf(", "); } }
return 0;}
Output
Enter the number of terms: 15 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
Fibonacci Series using Array in C
Problem Statement: Create a Fibonacci Series to find the first 20 Fibonacci Numbers using arrays in C.
#include <stdio.h>
int main() { int i, n;
// Ask the user for the number of terms printf("Enter the number of terms: "); scanf("%d", &n);
if(n <= 0) { printf("Please enter a positive integer.\n"); return 1; // Return an error code }
// Declare an array of size n int fibonacci[n];
// Initialize the first two terms fibonacci[0] = 0; if (n > 1) { fibonacci[1] = 1; }
// Generate the Fibonacci series for (i = 2; i < n; i++) { fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; }
// Print the Fibonacci series printf("Fibonacci Series: "); for (i = 0; i < n; i++) { printf("%d", fibonacci[i]); if (i < n - 1) { printf(", "); } }
return 0;}
Output
Enter the number of terms: 20 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
Until now, you clearly understand how to generate or create a Fibonacci Series in C. So, it’s time to level up. Let’s take a puzzle and use Fibonacci Series and C programming to solve the problem.
Case-Study: Stock Market Analysis
Problem Statement: Traders often use Fibonacci retracement levels to predict future price movements in the stock market. Suppose you are a trader who wants to analyze the price movements of a particular stock. You could use the Fibonacci series to calculate potential retracement levels and make trading decisions based on that.
Let’s first understand what Fibonacci Retracement is.
Fibonacci retracement levels are horizontal lines indicating potential support and resistance levels. They are calculated by taking two extreme points (usually a peak and a trough) on a stock chart and dividing the vertical distance by the key Fibonacci ratios of 23.6%, 38.2%, 50%, 61.8%, and 100%.
Let’s look at how it can be done in C programming.
#include <stdio.h>
int main() { // Declare variables for the high, low and difference float high, low, diff;
// Ask the user for the high and low of the stock printf("Enter the high of the stock: "); scanf("%f", &high); printf("Enter the low of the stock: "); scanf("%f", &low);
// Calculate the difference diff = high - low;
// Calculate and print the Fibonacci retracement levels printf("Fibonacci Retracement Levels:\n"); printf("23.6%%: %.2f\n", high - diff * 0.236); printf("38.2%%: %.2f\n", high - diff * 0.382); printf("50.0%%: %.2f\n", high - diff * 0.5); printf("61.8%%: %.2f\n", high - diff * 0.618); printf("100.0%%: %.2f\n", low);
return 0;}
Output
Enter the high of the stock: 36572 Enter the low of the stock: 36000 Fibonacci Retracement Levels: 23.6%: 36437.01 38.2%: 36353.50 50.0%: 36286.00 61.8%: 36218.50 100.0%: 36000.00
Related Reads:
Conclusion
Thus, through the development of this Fibonacci program in C, we have not only successfully created a tool that generates a Fibonacci sequence up to a user-specified term, but we have also delved deep into the efficiency and optimization of algorithms in programming.
FAQs
What is Fibonacci Series?
Fibonacci numbers are the sequence of numbers defined by the linear equation, F(n) = F(n-1) + F(n-2), for n = 3, 4, ... and F(0) = 0, F(1) = F(2) = 1.
What are the first 10 Fibonacci Numbers in the Fibonacci Series?
The first 10 Fibonacci numbers are, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.
What is the use of Fibonacci Series?
Fibonacci Series is used in Finance (Stock Market Analysis), Computer Science (Efficient Search), Music (Tuning Musical Instruments), Art (Creating Pleasing Visual Design).
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio