fibonacci sequence java

Java
//Fibonacci Series using Recursion 
#include<bits/stdc++.h> 
using namespace std; 
  
int fib(int n) 
{ 
    if (n <= 1) 
        return n; 
    return fib(n-1) + fib(n-2); 
} 
  
int main () 
{ 
    int n = 9; 
    cout << fib(n); 
    getchar(); 
    return 0; 
} 
  
// This code is contributed 
// by Akanksha Rai 
/*
Author: Jeffrey Huang
This finds the fibonacci number using a term provided by a user
A recursive method is used.

b)
9 calls are made to the fibonacci sequence in order to compute the 5th term.

*/
import java.io.*;
public class Fibonacci_JeffreyHuang
{
    //static int counter=0;

    public static long Fibonacci (long n)
    {
        //count++;
        if (n == 1 || n == 2)
        {
            return 1;
        }
        else if (n <= 0)
        {
            System.out.println ("Error: There is no term less than 1");
            return 0;
        }
        else
        {
            return (Fibonacci (n - 1) + Fibonacci (n - 2));
        }
    }


    public static void main (String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        String sentinel = "yes";
        long fib_term = 0;
        boolean invalid;

        while (sentinel.equalsIgnoreCase ("yes") || sentinel.equalsIgnoreCase ("y"))
        {
            //input
            System.out.println ("Which fibonacci term would you like to find?");
            invalid = true;
            while (invalid == true)
            {
                invalid = false;
                try
                {
                    fib_term = Integer.parseInt (br.readLine ());
                    /*
                    while (fib_term<=0)
                    {
                        System.out.println ("Invalid input. Please try again.");
                        System.out.println ();
                        fib_term = Integer.parseInt (br.readLine ());
                    }
                    */
                }
                catch (Exception e)  //in case the string is not a number
                {
                    System.out.println ("Invalid input. Please try again.");
                    System.out.println ();
                    invalid = true;
                }
            }

            //output
            System.out.println ();
            if (fib_term <= 0)
            {
                Fibonacci (fib_term);
            }
            
            else
            {
                System.out.println ("Term " + fib_term + " of the fibonacci sequence is " + Fibonacci (fib_term));
            }
            //System.out.println(counter);

            //rerun program
            System.out.println ();
            System.out.println ("Would you like to run the program again? (y/n)");
            System.out.println ();
            sentinel = br.readLine ();
            System.out.println ();

            while (!(sentinel.equalsIgnoreCase ("yes") || sentinel.equalsIgnoreCase ("y")
                        || sentinel.equalsIgnoreCase ("n") || sentinel.equalsIgnoreCase ("no")))
            {
                //System.out.println();
                System.out.println ("Invalid input. Please try again.");
                System.out.println ("Would you like to run the program again? (y/n)");
                sentinel = br.readLine ();
                System.out.println ();
            }
        }

        System.out.println ("Program terminated by user.");

    }
}# Function for nth Fibonacci number 
  
def Fibonacci(n): 
    if n<0: 
        print("Incorrect input") 
    # First Fibonacci number is 0 
    elif n==0: 
        return 0
    # Second Fibonacci number is 1 
    elif n==1: 
        return 1
    else: 
        return Fibonacci(n-1)+Fibonacci(n-2) 
  
# Driver Program 
  
print(Fibonacci(9)) 
  
#This code is contributed by Saket Modi 
//Fibonacci Series using Recursion 
#include<stdio.h> 
int fib(int n) 
{ 
   if (n <= 1) 
      return n; 
   return fib(n-1) + fib(n-2); 
} 
  
int main () 
{ 
  int n = 9; 
  printf("%d", fib(n)); 
  getchar(); 
  return 0; 
} 
// C# program for Fibonacci Series  
// using Recursion 
using System;  
  
public class GFG  
{  
    public static int Fib(int n)  
    {  
        if (n <= 1)  
        {  
            return n;  
        }  
        else
        {  
            return Fib(n - 1) + Fib(n - 2);  
        }  
    }  
          
    // driver code 
    public static void Main(string[] args)  
    {  
        int n = 9; 
        Console.Write(Fib(n));  
    }  
}  
  
// This code is contributed by Sam007 

Source

Also in Java: