/* * Fibonacci.java by Tue B. Jensen * Print the n'th fibonacci number, * Take the input from the command line. * Measure time elapsed during calculation. */ import java.io.*; class Fibonacci { public static long fib(int n) throws Exception { if (n < 0) throw(new Exception("Error n<0")); switch (n) { case 0: return 0; case 1: return 1; default: return fib(n-1)+fib(n-2); } } public static void main(String argv[]) { if (argv.length != 1) { System.out.println("Usage ex: java Fibonacci 5"); System.exit(1); } int n = 0; try { n = Integer.parseInt(argv[0]); } catch (Exception e) { System.out.println("Error: Integer argument expected.\n"+ "Usage ex: java Fibonacci 5"); System.exit(2); } try { long start, stop, result; start = System.currentTimeMillis(); result = fib(n); stop = System.currentTimeMillis(); System.out.println("fib("+n+") Running Time: "+(stop-start)+"ms"); System.out.println("fib("+n+") = "+result); } catch (Exception e) { System.out.println(e); } } }