Guest User

Untitled

a guest
Jun 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. /**
  2. This method demos the recursive fib method.
  3. */
  4.  
  5. public class Project1
  6. {
  7. public static void main(String [] args)
  8. {
  9. System.out.println("The first 20 numbers in " +
  10. "the Fibonacci series are: ");
  11.  
  12. for (int i=0;i<20;i++)
  13. System.out.print(fib(i) + " ");
  14.  
  15. System.out.println();
  16. }
  17.  
  18. /**
  19. The Fib method calculates the nth number
  20. in the Fibonacci series.
  21. @parm n The nth number to calculate.
  22. @return The nth number.
  23. */
  24.  
  25. public static int fib(int n)
  26. {
  27. if (n== 0)
  28. return 0;
  29. else if (n==1)
  30. return 1;
  31. else
  32. return fib(n-1) + (n-2);
  33. }
  34. }
Add Comment
Please, Sign In to add comment