Skip to content

Instantly share code, notes, and snippets.

@TuHuynhVan
Created February 4, 2020 05:26
Show Gist options
  • Save TuHuynhVan/829e7b656fab734b828768f79294fdc6 to your computer and use it in GitHub Desktop.
Save TuHuynhVan/829e7b656fab734b828768f79294fdc6 to your computer and use it in GitHub Desktop.
package practice;
/**
* Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13...
* n: 1, 2, 3, 4, 5, 6 Input an number n.
* Output the Fibonacci number with that position
*/
public class FibonacciFinder {
public static void main(String[] args) {
System.out.println("Fibonacci at position -1: " + findFibonacciNumber(-1));
System.out.println("Fibonacci at position 0: " + findFibonacciNumber(0));
System.out.println("Fibonacci at position 3: " + findFibonacciNumber(3));
System.out.println("Fibonacci at position 6: " + findFibonacciNumber(6));
}
public static Integer findFibonacciNumber(int n) {
if (n <= 0) {
System.out.println("Please input a positive interger number");
return null;
}
if (n == 1)
return 0;
if(n==2)
return 1;
return findFibonacciNumber(n - 1) + findFibonacciNumber(n - 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment