In this tutorial, We will learn Fibonacci Series in C programming language. Fibonacci Series generates the next number by adding two last numbers.
We’ll be covering the following topics in this tutorial:
What is Fibonacci Series in C?
Fibonacci Series in C: The Fibonacci Sequence is the sequence of numbers where the next term is the sum of the previous two terms. The first two terms in the Fibonacci series are 0, accompanied by 1. The Fibonacci sequence: 0 , 1, 1, 2, 3, 5, 8, 13, 21.
Algorithm of Fibonacci Series
START
Step 1 → Enter int variable A, B, C
Step 2 → Set A = 0, B = 0
Step 3 → DISPLAY A, B
Step 4 → C = A + B
Step 5 → DISPLAY C
Step 6 → Set A = B, B = C
Step 7 → REPEAT from 4 - 6, for n times
STOP
Pseudocode of Fibonacci series
procedure fibonacci : fibonacci_number IF fibonacci_number less than 1 DISPLAY 0 IF fibonacci_number equals to 1 DISPLAY 1 IF fibonacci_number equals to 2 DISPLAY 1, 1 IF fibonacci_number greater than 2 Pre = 1, Post = 1, DISPLAY Pre, Post FOR 0 to fibonacci_number-2 Fib = Pre + Post DISPLAY Fib Pre = Post Post = Fib END FOR END IF end procedure
Fibonacci Series C Programs
Let us denote ith term in the Fibonacci series as Fi, where i is assumed to take values starting from 0. Thus, the first four terms in the Fibonacci series are denoted as F0, F1, F2. and F3. The first two terms are given as F0 = 0 and F1 = 1.
Each subsequent term is obtained by adding two previous terms in the series. Thus, the first 10 terms in the series are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The equation for term Fi, i > 2, can be expressed in the form of a recurrence relationship as Fi = Fi -1+ Fi – 2.
Let us use the variable term to represent term Fi in the Fibonacci series and variables prev1 and prev2 to define previous terms (Fi-1 and Fi-2) as shown in the prev2 prev1 term for i = 2. Now the F2 can be obtained as
term = prev1 + prev2;
To determine the next term in the series, first move prev1 and prev2 to next position as shown in using the following statements:
prev2 = prev1;
prev1 = term;
Now term F3 can be calculated using the same statement given above (term = prev1 + prev2). We can determine the subsequent terms in the series by repeating the above procedure. A program to determine and print the first n terms in the Fibonacci series is given below.
#include<stdio.h> int old_number; int current_number; int next_number; int main() { old_number = 1; current_number = 1; clrscr(); printf("Fibonacci Series are : \n"); printf("1 "); while (current_number<100) { printf(" %d ", current_number); next_number = current_number + old_number; old_number = current_number; current_number = next_number; } getch(); return (0); }
Fibonacci Series Program in C Using for Loop
/* Fibonacci Series c language */
#include<stdio.h> void main() { int n, first = 0, second = 1, next, c; clrscr(); printf("Enter the Number of Terms : "); scanf("%d",&n); printf("First %d terms of Fibonacci Series are : ",n); for (c = 0;c < n;c++) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } printf(" %d ",next); } getch(); }
Fibonacci Series Using Recursion in C
#include<stdio.h> int fib(int m); void main() { int i,n; clrscr(); printf("\n Enter no: of Terms = "); scanf("%d",&n); for(i = 1;i <= n;i++) { printf(" %d ",fib(i)); } getch(); } int fib(int m) { if(m == 1 || m == 2) { return(1); } else { return(fib(m-1)+ fib(m-2)); } }
Explanation: In this program, recursion is used because the Fibonacci number n is generated to the sum of its last number
fib (m) = fib (m-1) + fib (m-2)
Here fib () is a function that computes nth Fibonacci number. The exit criteria are that if m==1 then return 0 and if the exit criteria are m==2, then return 1.
C Program to print Fibonacci Series upto N number
#include<stdio.h> #include<conio.h> void main() { int a=0, b=1, c, n, count; printf("How many fibonacci numbers :"); scanf ("%d", &n); if (n==1) printf ( "0"); else if (n==2) printf ( "1" ) ; else { printf ("%d\t%d" ,a,b); count=3; while(count<=n) { c = a+b; printf ( "\t%d", c); a = b; b = c; count++; } } getch(); }
Explanation: On execution, the user is prompted to enter the number of terms Fibonacci series (n = say 12) that need to be printed. As n = 12, so the expression if (n==0) evaluates to false,and the control shifts to else if part. As the expression if(n==1) also evaluates to false, the control shifts to the next part.
The statements in its body are executed. First, the first two Fibonacci numbers are printed. Then we set count=3. Now, as we have to print first n Fibonacci numbers, so we take a while loop as shown.
We know that,
Fibonacci (n) = fibonacci(n-1) + fibonacci(n-2)
Therefore, we go on adding the previous numbers and print the result. To proceed, we assign the value stored in b to a, and the value of c is assigned to b. The value of count is incremented by 1 on each such operation. The process continues until the condition remains satisfied.