In this tutorial I am going to show you how printing a Fibonacci series using C Language for a given input using scanf. First we have to learn about Fibonacci Series of a given number. According to Wikipedia “In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones”.
Example – Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, ….
Formula for Fibonacci Sequence /Series :
The Fibonacci numbers are generated by setting F0=0, F1=1, and then using the recursive formula Fn = Fn-1 + Fn-2
C Program to find out Fibonacci Series / Sequence :
Create a file fibonacci.c with following code then compile and run .
Editor Used – Geany, compiler – GCC, OS – Linux Min 18.1
You can use your favorite editor and compiler like Turbo C , Borland, Notepad++
#include <stdio.h>
int main(){
int terms;
int i,n1,n2,n;
n1=0;
n2=0;
n=1;
printf(“\nEnter a number of terms of Fibonacci Series:”);
scanf(“%d”,&terms);
for(i=0;i<terms;i++){
printf(“%d”,n);
n1=n2;
n2=n;
n=n1+n2;
}
return 0;
}