Java Recursion Program Practise 2 - Fibonacci Series

Java program to find the given term of Fibonacci Series using Recursion Function

Output

import java.util.*;
class Main {
    
    int fib(int n){
        if(n==1){
        return 0;
        }
        if(n==2){
        return 1;
        }
        if(n>2){
            return(fib(n-2)+fib(n-1));
        }
        else
        return -1;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a;
        System.out.println("Enter the number of terms of Fibonacci series to be displayed using Recursion function");
        a = in.nextInt();
        Main ob = new Main();
       System.out.println(ob.fib(a));
        
        
        
    }
}

Comments

Translate

Popular posts from this blog

Letter to SBI Bank Manager of Local Branch to reduce the interest rate on home loan

Summers are becoming hotter with each passing year. Write a description of one such very hot day. What did you see and hear as you walked outside? How were birds and animals affected?

Narrate an experience that shows appearances can be deceiving

"No other subject taught in school is as important as Moral Science." Express your views for or against this statement

Write a short story entitled "Advice not taken"

Imagine a situation where you get an opportunity to change one thing in your school. What would it be? Why do you want to change it? How would you bring about the change?

Write a story with two students and a teacher as main characters

Abhisara - the Tryst Notes

You visited a heritage site with your classmates and teachers. Describe what you saw and learned from your visit.

Java String Manipulation 28th Jan Practice 2 - input a string and check whether it is a palindrome or not