Created
December 21, 2016 22:51
-
-
Save AzureKitsune/b519d9d609c54d7f355ade105f0e24ea to your computer and use it in GitHub Desktop.
A simple program for performing the fibonacci sequence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fib; | |
import java.util.Scanner; | |
public class App { | |
@SuppressWarnings("resource") | |
public static void main(String[] args) { | |
Scanner stdIn = new java.util.Scanner(System.in); | |
while (true) { | |
try { | |
System.out.print("Enter a number thats greater or equal to 0: "); | |
int number = stdIn.nextInt(); | |
int result = fib(number); | |
System.out.println("Result: " + result); | |
} catch (Exception e) { | |
System.out.println("Error: " + e.getMessage()); | |
} | |
} | |
} | |
@SuppressWarnings("unused") | |
private static int fib(int value) throws Exception { | |
if (value < 0) throw new Exception("Value out of range."); | |
if (value == 0 || value == 1) { | |
return 1; | |
} else { | |
return fib(value - 1) + fib(value - 2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment