Fibonacci Series Program in C: A Step-by-Step Guide

TechDyer

A series of numbers known as the Fibonacci series, which typically starts with 0 and 1, has each number equal to the sum of the two numbers that came before it. The simple yet profound properties of this sequence make it a popular topic in mathematics and computer science. We’ll demonstrate how to write a Fibonacci Series Program in C programming language in this article.

What is the Fibonacci Series Program in C?

Using one of the C loops, this method iterates and prints the current term. The first two terms (F1 and F2) are addressed separately. The next two terms are then stored in two variables, which are updated as we print the next term.

 

For example, a Fibonacci series that starts with 0 and 1 is as follows:

 

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

How to Construct a Fibonacci Series Program in C Language with Recursion?

In this method, we will use a function that prints the first two terms. Another function handles the remaining terms by printing the subsequent terms in the sequence using a recursive technique.

 

// C Program to print the Fibonacci series using recursion

#include <stdio.h>

 

// first two values

int prev1 = 1;

int prev2 = 0;

 

// recursive function to print the Fibonacci series

void fib(int n)

{

if (n < 3) {

return;

}

int fn = prev1 + prev2;

prev2 = prev1;

See also  Understanding Substring in Python: A Comprehensive Guide

prev1 = fn;

printf(“%d “, fn);

return fib(n – 1);

}

 

// function that handles the first two terms and calls the

// recursive function

void printFib(int n)

{

// when the number of terms is less than 1

if (n < 1) {

printf(“Invalid number of terms\n”);

}

// when the number of terms is 1

else if (n == 1) {

printf(“%d “, 0);

}

// when the number of terms is 2

else if (n == 2) {

printf(“%d %d”, 0, 1);

}

// number of terms greater than 2

else {

printf(“%d %d “, 0, 1);

fib(n);

}

return;

}

 

// driver code

int main()

{

int n = 9;

printFib(n);

return 0;

}

Fibonacci Series Program in C (Iteration)

This method iterates and prints the current term using one of the C loops. F1 and F2, the first two terms, are dealt with independently. The next two terms are then stored in two variables, which we maintain updated while printing the subsequent term.

 

// C Program to print the Fibonacci series using iteration

// (loops)

#include <stdio.h>

 

// function to print Fibonacci series

void printFib(int n)

{

if (n < 1) {

printf(“Invalid Number of terms\n”);

return;

}

 

// when number of terms is greater than 0

int prev1 = 1;

int prev2 = 0;

 

// for loop to print Fibonacci series

for (int i = 1; i <= n; i++) {

if (i > 2) {

int num = prev1 + prev2;

prev2 = prev1;

prev1 = num;

printf(“%d “, num);

}

 

// for first two terms

if (i == 1) {

printf(“%d “, prev2);

}

if (i == 2) {

printf(“%d “, prev1);

}

}

}

 

// driver code

int main()

See also  How to End Program in Python? 

{

 

int n = 9;

printFib(n);

return 0;

}

How Does a Program Compute Fibonacci Function?

It’s critical to first grasp the underlying idea of the Fibonacci sequence to fully appreciate the mechanics of how a program computes the Fibonacci series. Starting with 0 and going up to 1, the Fibonacci sequence is a series of numbers in which each number is equal to the sum of the two numbers that came before it. Formally, the relation defines the sequence:

F(n) = F(n-1) + F(n-2)

where the nth term in the Fibonacci sequence is denoted by F(n).

Now let’s examine how an iterative method (a ‘for’ loop, for instance) and a recursive method are used in software to compute the Fibonacci function.

  • Iterative Approach
    • Loop through the Sequence: After that, the program launches an n-time loop.
    • Initialise Variables: The initial values of two variables (t1 and t2), which correspond to the first two numbers in the Fibonacci sequence, are 0 and 1, respectively.
    • Input From User: Users are prompted by the program to enter the desired number of Fibonacci sequence terms to be calculated. The variable n contains this value.
    • Calculate the Next Term: The sum of t1 and t2 is computed inside the loop to determine the next term, and this result is saved in a new variable called next term.
    • Update Variables: To prepare for the upcoming iteration, the variables t1 and t2 are updated. t2 receives the value of nextTerm, and t1 receives the current value of t2.
    • Print the Term: The loop continues to the next iteration until it has run n times, at which point the current term (t1) is printed.
  • Recursive Approach
    • Print the Result: Next, each function call’s outcome is printed, providing the Fibonacci series.
    • Base Cases: We specify what ought to occur for our base cases, n = 0 and n = 1, inside the function. The function should yield a result of 0 for n = 0 and a result of 1 for n = 1.
    • Recursive Call: The function calls itself twice, once with argument n-1 and once with n-2, if n is neither 0 nor 1. The total of these two calls is then returned by the function.
    • Function Definition: According to its definition, the Fibonacci function accepts an integer n as an input. The position in the Fibonacci sequence that this argument represents is the one for which we are looking for the number.
    • Invoke the Recursive Function: We use the argument that corresponds to the position in the sequence for which we want to find the number to call our Fibonacci function from our main program. For as many terms as we want to calculate, we repeat this.
See also  How to Multiply in Python?

Conclusion

The Fibonacci series is a sequence in which every number is equal to the sum of the two numbers that came before it, starting with 0 and 1. This post describes how to use both recursion and iteration to implement the Fibonacci Series Program in C. It highlights the advantages of each approach by going over the basic ideas, providing code samples, and outlining the specific procedures for each.

Read more

Share This Article
Follow:
I'm a tech enthusiast and content writer at TechDyer.com. With a passion for simplifying complex tech concepts, delivers engaging content to readers. Follow for insightful updates on the latest in technology.
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *