Fibonacci Series In C : A Quick Start To C Programming

Last updated on Mar 29,2022 121.6K Views

Fibonacci Series In C : A Quick Start To C Programming

edureka.co

If you have attended interviews as a programmer, you would know that there many C programming interviews that may a question to create a program for Fibonacci series . This tricky question which seems simple bothers many. In this article we would be discussing How to implement Fibonacci Series in C. Following pointers will be discussed here,

Let us get started then,

Fibonacci Series in C

Fibonacci series is a series of numbers formed by the addition of the preceding two numbers in the series. The first two terms are zero and one respectively. The terms after this are generated by simply adding the previous two terms.

There are two ways to write the fibonacci series program:

How do you calculate Fibonacci?

The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

The next number is found by adding up the two numbers before it:

Here is an example of Fibonacci series: 0,1,1,2,3,5,8,13….etc.

In the above example, 0 and 1 are the first two terms of the series. These two terms are printed directly. The third term is made by adding the first two terms. In this case 0 and 1. So, we get 0+1=1. Hence 1 is printed as the third term. The next term is generated by using the second and third term and not using the first term. It is done until the number of terms you want or requested by the user. In the above example, we have used eight terms.

Here is a c program:


#include<stdio.h>
int main()
{
int first=0, second=1, i, n, sum=0;
printf("Enter the number of terms: ");
scanf("%d",&n);
//accepting the terms
printf("Fibonacci Series:");
for(i=0 ; i<n ; i++)
{
if(i <= 1)
{
sum=i;
}
//to print 0 and 1
else
{
sum=first + second;
first=second;
second=sum;
//to calculate the remaining terms.
//value of first and second changes as new term is printed.
}
printf(" %d",sum)
}
return 0;
}

Output:

In the above program, we first declare all variables. First, we set the values for first and second, these will be the variables we will use to generate further terms. Next, we declare the term n, that will hold the number of terms. We have a  term to hold the sum of the two digits called sum. The last term is i. It is used for iteration in the for loop.

We accept the number of terms from the user and store it in n. We then have a for loop that runs from 0 all the way to the number of terms requested by the user, that is n.

Inside the for loop, we first have an if statement with the condition checking if the value of i if it is less then 1. If it is zero or one is printed, depending on the number of terms. It is used to print the initial zero and one when there are more than two terms.

If the number of terms is greater then one, the else part of the loop is executed. In this part, the addition of the variable first and second is assigned to the variable sum. The next term is the sum variable. For example, first and second whose values are 0 and 1 are added to get the sum value as 1.

In the next part, we assign the value of the second term to the first term and after that, the value of sum to the second term. This is done because for the next term the previous two values are changed as a new value is printed. This is the sum value. If we consider 0 and 1 assigned to first and second, after this step the value of first will be 1 and the value of the second will also be 1 because the value of sum is 1.

After exiting the else part we print the sum value. This is executed until the value of i becomes equal to n. The loop breaks and we exit the program.

Let us continue with this Fibonacci series in C article and see what else can be done with it,

Fibonacci Series Till A User Enters Number

Code:


#include <stdio.h>
int main()
{
int first = 0, second = 1, sum = 0, n;
printf("Enter the end term for the series: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", first, second);
sum = first + second;
while(sum <= n)
{
printf("%d, ",sum);
first = second;
second = sum;
sum = first + second;
}
return 0;
}

Output:

In this program, we take the end term from the user. We must display a Fibonacci series up to that number. This is done by using a while loop. We take input from the user which is the last term. Then print the first and second terms. After this, add first and second and store it in sum. Then, there is a while loop. It runs till the value of the sum is less than that of the number entered by the user. Inside the while loop, Print out the sum first.

In the next part, we assign the value of the second term to the first term and after that, the value of sum to the second term. We perform addition again adding first and second term and assigning it to sum. The loop runs till the sum value is greater than the number entered by the user.

What is the Fibonacci of 10?

The Fibonacci sequence is achieved by adding the two previous numbers to get the next one, starting with 0 and 1:

#include <iostream>
using namespace std;
int main ()
{
int a = 0, b = 1;
cout << a << ", " << b;
for (int i = 0; i < 8; i++)
{
cout << ", " << a + b;
b = a + b; // b is the sum of the 2 numbers
a= b - a; // a is the old y
}
}

Is 0 a Fibonacci number?

Yes, The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Let us move on to the final bit of this Fibonacci Series in C article.

Fibonacci Series Using Recursion

Another way to program the Fibonacci series generation is by using recursion. Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Code:


#include<stdio.h>
int f(int);
int main()
{
int n, m= 0, i;
printf("Enter Total terms:n");
scanf("%d", &n);
printf("Fibonacci series terms are:n");
for(i = 1; i <= n; i++)
{
printf("%dn", fibonacci(m));
m++;
}
return 0;
}
int fibonacci(int n)
{
if(n == 0 || n == 1)
return n;
else
return(fibonacci(n-1) + fibonacci(n-2));
}

Output:

In this program we use recursion to generate the fibonacci series.  The function fibonacci is called recursively until we get the output. In the function, we first check if the number n is zero or one. If yes, we return the value of n. If not, we recursively call fibonacci with the values n-1 and n-2.

These are the ways of generating a Fibonacci series in C.

With this we come to the end of this blog on ‘Leap Year Program In C’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this article and we will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework