Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Fibonacci {
- public static int fibonacci(int n) {
- if (n<0)
- throw new IllegalArgumentException("Fibonaccizahlen sind fΓΌr negativen Index nicht definiert.");
- else if (n == 0)
- return 0;
- else if (n == 1)
- return 1;
- else
- return fibonacci(n - 1) + fibonacci(n - 2);
- }
- public static void main(String[] args) {
- if (args.length !=1) {
- System.out.println("Aufrug: java de.kaiguenster.javaintro.fibonacci.Fibonacci <n>");
- System.exit(1);
- }
- int n = Integer.parseInt(args[0]);
- int result = fibonacci(n);
- System.out.println("Die " + n + ". Fibonacci-Zahl ist: " + result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement