Print Fibonacci Series using recursion

Problem statement:- Program to Print Fibonacci Series using Recursion.

Data requirement:-

   Input Data:- n

  Output Data:-
FibonacciSeries(i)

Program in C

Here is the source code of the C Program to print the Fibonacci Series using recursion.

Code:

#include<stdio.h>
int FibonacciSeries(int n)
{
    if(n==0)
        return 0;
    else if(n==1)
        return 1;
    else
        return FibonacciSeries(n-1)+FibonacciSeries(n-2);
}
int main()
{
    int n,i;
    printf("Enter the Limit:");
    scanf("%d",&n);
    printf("All Fibonacci Numbers in the given Range are:");
    for(i=0;i<n;i++)
    {
        printf("%d ",FibonacciSeries(i));
    }
}

Input/Output:
Enter the Limit:5
All Fibonacci Numbers in the given Range are:0 1 1 2 3

Program in C++

Here is the source code of the C++ Program to Print Fibonacci Series using recursion.

Code:

#include<iostream>
using namespace std;
int FibonacciSeries(int n)
{
    if(n==0)
        return 0;
    else if(n==1)
        return 1;
    else
        return FibonacciSeries(n-1)+FibonacciSeries(n-2);
}
int main()
{
    int n,i;
    cout<<"Enter the Limit:";
    cin>>n;
    cout<<"All Fibonacci Numbers in the given Range are:";
    for(i=0;i<n;i++)
    {
        cout<<FibonacciSeries(i)<<" ";
    }
}

Input/Output:
Enter the Limit:8
All Fibonacci Numbers in the given Range are:0 1 1 2 3 5 8 13

Program in Java

Here is the source code of the Java Program to Print Fibonacci Series using recursion.

Code:

import java.util.Scanner;
public class FibonacciSeriesPrint {
static int FibonacciSeries(int n)
{
    if(n==0)
        return 0;
    else if(n==1)
        return 1;
    else
        return FibonacciSeries(n-1)+FibonacciSeries(n-2);
}
public static void main(String[] args) {
           Scanner cs=new Scanner(System.in);
           int n,i;
    System.out.print("Enter the Limit:");
    n=cs.nextInt();
    System.out.print("All Fibonacci Numbers in the given Range are:");
    for(i=0;i<n;i++)
    {
    System.out.print(FibonacciSeries(i)+" ");
    }
        cs.close();
}
}

Input/Output:
Enter the Limit:10
All Fibonacci Numbers in the given Range are:0 1 1 2 3 5 8 13 21 34 

Program in Python

Here is the source code of the Python program to the Print Fibonacci Series using recursion.

Code:

def FibonacciSeries(n):
    if n==0:
        return 0
    elif(n==1):
        return 1
    else:
        return FibonacciSeries(n-1)+FibonacciSeries(n-2)
n=int(input("Enter the Limit:"))
print("All Fibonacci Numbers in the given Range are:")
for i in range(0,n):
    print(FibonacciSeries(i),end=" ")

Input/Output:

Post a Comment

0 Comments