Fibonacci Program in C

Fibonacci Program in C

4 mins read248 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 9, 2023 18:45 IST

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.

2023_02_Feature-Image-Templates-70.jpg

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
Fibonacci Series in Java [Case-Study Based]
Fibonacci Series in Java [Case-Study Based]
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In Java, the Fibonacci...read more
Fibonacci Series Program in C++
Fibonacci Series Program in C++
The Fibonacci series is an intriguing mathematical idea that is popular for practicing in Python, especially for those just starting out.
Fibonacci Series in Python
Fibonacci Series in Python
Fibonacci Series is a sequence of numbers where each number is the sum of the two previous numbers. This article will discuss the Fibonacci Series, How to Create a Fibonacci...read more

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;
}
Copy code

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;
}
Copy code

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;
}
Copy code

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;
}
Copy code

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:

10 Must-Read C Programming Books of All Time
10 Must-Read C Programming Books of All Time
This list of the top C programming books has been curated from several prestigious publications in computing literature, discussions on C programming communities across Reddit, Y Combinator, and bestsellers on...read more
C programming examples 
C programming examples 
If you want to learn C language by executing programming examples. Then this article offers you 17C programming examples with outputs, so that you can learn fast.
What is Interpreter: Types, Advantages and Disadvantages
What is Interpreter: Types, Advantages and Disadvantages
Interpreter is a type of computer program that directly executes instructions written in a programming or scripting language.
Concatenate Strings with strcat() Function in C
Concatenate Strings with strcat() Function in C
This article offers a comprehensive explanation of the syntax and implementation of the strcat() function, in addition to providing several examples to aid in understanding its operation. By the end...read more
Explore toupper() Function in C with Examples
Explore toupper() Function in C with Examples
The toupper() function takes a single character as input and returns the corresponding uppercase character. It is commonly used in conjunction with string manipulation functions to convert entire strings to...read more
All About Strlen() Function in C
All About Strlen() Function in C
Unveil the strlen() function in C programming, diving deep into its syntax, practical implementations, and code samples. Learn how to effectively utilise strlen() to determine string lengths, manipulate strings, and...read more
Methods to Calculate Length of String in C
Methods to Calculate Length of String in C
Calculating the length of a string is a very common task in C programming, and there are several methods to accomplish this task. In C programming language, strlen() is a...read more
Adding Two Matrices in C
Adding Two Matrices in C
A matrix is a multidimensional array of numbers organized in rows and columns. Matrices can be manipulated using operations like Addition, Subtraction, and Multiplication. Adding two matrices in C, is...read more
What are C rand() and srand() Functions?
What are C rand() and srand() Functions?
The purpose of this blog is to make you understand the rand() and sand() C programming functions. Let’s learn about them with the help of easy examples.

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).

About the Author
author-image
Vikram Singh
Assistant Manager - Content

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