C Program to Compute Nth Fibonacci Number

Previous Article
Next Article

There are many important mathematical series and one of them is the Fibonacci series. It is used for learning recursion in programming. Recursion means a function recursively calls itself during the execution of a program.

This program is compiled using Dev-C++ version 4 compiler on a Windows 7 64-bit system, you can use any other standard C compiler to compile the program and it will work.

This article is intended for intermediate learners of C programming language, so you must be familiar with basic C concepts like functions, variables, operators, and expressions.

Problem Definition

In mathematics, the whole number means all positive numbers starting with 0. Any term in Fibonacci series takes a sum of previous two terms. The formula for the nth term of Fibonacci series is given below.


fn = fn-1+ fn-2


The first few terms of the series are as follows

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


Flowchart - Program for Fibonacci Series
Flowchart – Program for Fibonacci Series

Program Code

/* Program to calculate Fibonacci Series */
# include < stdio.h >
#include < stdlib.h >
main ()
int n;
void fib ();
printf ("Number of terms to be generated?");
scanf ("%d", & n);
printf ("%d", n);
printf ("\n\nFibonacci sequence up to %d terms :\n\n");
return 0;
void fib(int n)
static long int f1 = 0, f2 = 1, sum;
if (n > 0)
sum = f1 + f2;
f1 = f2;
printf("%5d", sum);
f2 = sum;
fib(n - 1);
} }


The output of the Fibonacci series is given below.

Output - Fibonacci Series
Output – Fibonacci Series
Previous Article
Next Article