Fibonacci Series Java Example

  1. /*
  2. Fibonacci Series Java Example
  3. This Fibonacci Series Java Example shows how to create and print
  4. Fibonacci Series using Java.
  5. */
  6.  
  7. public class JavaFibonacciSeriesExample {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. //number of elements to generate in a series
  12. int limit = 20;
  13.  
  14. long[] series = new long[limit];
  15.  
  16. //create first 2 series elements
  17. series[0] = 0;
  18. series[1] = 1;
  19.  
  20. //create the Fibonacci series and store it in an array
  21. for(int i=2; i < limit; i++){
  22. series[i] = series[i-1] + series[i-2];
  23. }
  24.  
  25. //print the Fibonacci series numbers
  26.  
  27. System.out.println("Fibonacci Series upto " + limit);
  28. for(int i=0; i< limit; i++){
  29. System.out.print(series[i] + " ");
  30. }
  31. }
  32. }
  33.  
  34. /*
  35. Output of the Fibonacci Series Java Example would be
  36. Fibonacci Series upto 20
  37. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
  38. */

Thanks!!!!

Oh man I'm trying to figure this out for so many nights but still can't...
Well think I'm lucky enough to accidentally enter at your site.

I already add your site to my favorites.
Thanks!!
^_^

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!