Program to Display Fibonacci Series using Recursion

Description

The Fibonacci series is a sequence where the next term is the 
sum of the previous two terms. The first two terms of the Fibonacci
series are 0 followed by 1.

Example:
0 1 1 2 3 5 8 13 21 34...

C/C++

/* C Program to Display Fibonacci Series using Recursion*/
//Save it as FibonacciSeriesRecursion.c

#include<stdio.h>
int main(){

    int i, num, element=0;

    printf("Enter number of terms : ");
    scanf("%d",&num);

    printf("Fibonacci series is : \n");
    for(i=1;i<=num;i++){
        printf("%d ",fib(element));
        element++;
    }

    return 0;
}

int fib(int n){

    if(n<=1)
        return n;
    return fib(n-1)+fib(n-2);
}
Input:
Enter a number terms : 10

Output:
Fibonacci series is : 
0 1 1 2 3 5 8 13 21 34

Java

/* Java Program to Display Fibonacci Series using Recursion*/
//Save it as FibonacciSeriesRecursion.java

import java.io.*;
import java.util.Scanner;

public class FibonacciSeriesRecursion {

    public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);
        
            int i, num, element=0;

        System.out.println("Enter number of terms : ");
        num = scanner.nextInt();

        for(i=1;i<=num;i++){
            System.out.print(fib(element)+" ");
            element++;
        }
    }
    public static int fib(int n) {
        if(n<=1)
                return n;
        return fib(n-1)+fib(n-2);
    }
}
Input:
Enter a number terms : 
10

Output:
Fibonacci series is : 
0 1 1 2 3 5 8 13 21 34

Related Programs

1) Program to calculate power of a number
2) Program to calculate power of a number using recursion
3) Program to find HCF using Recursion
4) Program to calculate factorial using Recursion
5) Program to find sum of digits of a number
6) Program to reverse a number
7) Program to convert Binary to Decimal
8) Program to convert Octal to Decimal
9) Program to calculate square root of a number without using standard library function sqrt()
10) Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)
Share Me

Leave a Reply