From 084ba50c5aca6c5f1570539370c23322245278d0 Mon Sep 17 00:00:00 2001 From: KoalaKoya <72259949+KoalaKoya@users.noreply.github.com> Date: Fri, 2 Oct 2020 18:31:55 +0530 Subject: [PATCH] Create Fibonacci.java --- java/Fibonacci.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 java/Fibonacci.java diff --git a/java/Fibonacci.java b/java/Fibonacci.java new file mode 100644 index 0000000..18afb4f --- /dev/null +++ b/java/Fibonacci.java @@ -0,0 +1,26 @@ +/* This program uses a recursive function to find the number present at the given position in the Fibonacci series */ + +import java.util.*; + +public class Fibonacci { + public static long fib(long n) { + if ((n == 0) || (n == 1)) + return n; + else + return fib(n - 1) + fib(n - 2); + } + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + System.out.println("Please Enter the Position:"); + int pos=sc.nextInt(); + if (pos == 1) + System.out.println("The " + pos + "st fibonacci number is: " + fib(pos)); + if (pos == 2) + System.out.println("The " + pos + "nd fibonacci number is: " + fib(pos)); + if (pos == 3) + System.out.println("The " + pos + "rd fibonacci number is: " + fib(pos)); + else + System.out.println("The " + pos + "th fibonacci number is: " + fib(pos)); + } +}