Table of Contents
The program computes Nth Fibonacci number using a technique called recursion and prints the results. A recursion is the ability of a procedure or function to call itself several times.
You can write the program without recursion, but computer science students learn Fibonacci series as an introduction to recursion or recurrence relations. It is a topic of higher importance in both mathematics and computer science.
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.
\Large \begin{aligned}
& f_{n} = f_{n-1} + f_{n-2}
\end{aligned}The first few terms of the series are as follows
\Large \begin{aligned}
&0, 1, 1, 2, 3, 5, 8, 13, 21, 34, \cdots
\end{aligned}Flowchart – Program for Fibonacci Series

Program Codes – Fibonacci Series
C
C++
Java
Python
/* 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\n Fibonacci sequence up to %d terms :\n\n");
fib(n);
printf("\n");
system("PAUSE");
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);
}
}#include <iostream>
using namespace std;
void fib(int n) {
static long f1 = 0, f2 = 1;
long sum;
if (n > 0) {
sum = f1 + f2;
f1 = f2;
f2 = sum;
cout << sum << " ";
fib(n - 1);
}
}
int main() {
int n;
cout << "Number of terms to be generated?";
cin >> n;
cout << "\nFibonacci sequence up to " << n << " terms:\n\n";
fib(n);
cout << endl;
return 0;
}import java.util.Scanner;
class Fibonacci {
static long f1 = 0, f2 = 1;
static void fib(int n) {
if (n > 0) {
long sum = f1 + f2;
f1 = f2;
f2 = sum;
System.out.print(sum + " ");
fib(n - 1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Number of terms to be generated?");
int n = sc.nextInt();
System.out.println("\nFibonacci sequence up to " + n + " terms:\n");
fib(n);
System.out.println();
sc.close();
}
}f1, f2 = 0, 1
def fib(n):
global f1, f2
if n > 0:
s = f1 + f2
f1, f2 = f2, s
print(s, end=" ")
fib(n - 1)
n = int(input("Number of terms to be generated?"))
print(f"\nFibonacci sequence up to {n} terms:\n")
fib(n)
print()Output
Number of terms to be generated?:8
Fibnacci sequence upto 8 terms :
-------------------------------------------------------
1 2 3 5 8 13 21 34
-------------------------------------------------------