Skip to content

Instantly share code, notes, and snippets.

@AzureKitsune
Created December 21, 2016 22:51
Show Gist options
  • Save AzureKitsune/b519d9d609c54d7f355ade105f0e24ea to your computer and use it in GitHub Desktop.
Save AzureKitsune/b519d9d609c54d7f355ade105f0e24ea to your computer and use it in GitHub Desktop.
A simple program for performing the fibonacci sequence.
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