Java Program to Find Fibonacci of nth Number Using Recursion

This program will take input as n and return the Fibonacci of that number. Sample Input 1 5 Sample Output 1 5 Sample Input 2 13 Sample Output
Admin

 

This program will take input as n and return the Fibonacci of that number.


Sample Input 1
5
Sample Output 1
5
Sample Input 2
13
Sample Output 2
233
Solution
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.println(fibo(n));
    }
    public static int fibo(int n) {
        if(n==0||n==1)
        {
            return n;
        }
        int f1=fibo(n-1);
        int f2=fibo(n-2);
        return f1+f2;
    }
}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.