{"id":29,"date":"2018-10-08T10:16:00","date_gmt":"2018-10-08T10:16:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/10\/08\/program-to-display-fibonacci-numbers-in-java\/"},"modified":"2019-02-13T03:34:34","modified_gmt":"2019-02-13T02:34:34","slug":"program-to-display-fibonacci-numbers-in-java","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/program-to-display-fibonacci-numbers-in-java\/","title":{"rendered":"Program to Display Fibonacci Numbers in Java"},"content":{"rendered":"<p>Fibonacci Numbers or Fibonacci Series is a set of numbers where the next number in the series is the sum of the preceding two numbers.<\/p>\n<p>For example:<br \/>\n0, 1, 1, 2, 3, 5, 8, 13, 21 and so on.<br \/>\nNote that the first two numbers are set to 0 and 1<\/p>\n<p>&nbsp;<\/p>\n<p>I am going to present two different programs\u00a0 written in java to display the Fibonacci series:<br \/>\n<b>1. Fibonacci Series program using for loop without recursion<\/b><br \/>\n<b>2. Fibonacci Series program with recursion<\/b><\/p>\n<p>Both are written and tested using Eclipse IDE which you can download\u00a0 and install for free.<\/p>\n<p>&nbsp;<\/p>\n<p><b>1. Fibonacci Number in Java without recursion<\/b><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/* Program to Display Fibonacci Numbers<\/span>\r\n<span style=\"color: #888888;\"> * By: Kindson The Genius<\/span>\r\n<span style=\"color: #888888;\"> * Date: October 8, 2018<\/span>\r\n<span style=\"color: #888888;\"> * Usage: Free        *\/<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Fibonacci<\/span> {\r\n  <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>(String[] args) {\r\n\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> n1 = <span style=\"color: #6600ee; font-weight: bold;\">0<\/span>;\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> n2 = <span style=\"color: #6600ee; font-weight: bold;\">1<\/span>;\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> n3 = <span style=\"color: #6600ee; font-weight: bold;\">2<\/span>;\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> count = <span style=\"color: #6600ee; font-weight: bold;\">10<\/span>;\r\n\r\n   System.<span style=\"color: #008800; font-weight: bold;\">out<\/span>.print(n1 + <span style=\"background-color: #fff0f0;\">\" \"<\/span> + n2);\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">for<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> i = <span style=\"color: #6600ee; font-weight: bold;\">2<\/span>; i&lt;count; ++i)\r\n   {\r\n     n3 = n1 + n2;\r\n\r\n     System.<span style=\"color: #008800; font-weight: bold;\">out<\/span>.print(<span style=\"background-color: #fff0f0;\">\" \"<\/span> + n3);\r\n\r\n     n1 = n2;\r\n     n2 = n3;\r\n   }\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><b>2. Fibonacci Numbers in Java with recursion<\/b><br \/>\n<b><\/b><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/* Program to Display Fibonacci Numbers with recursion<\/span>\r\n<span style=\"color: #888888;\"> * By: Kindson The Genius<\/span>\r\n<span style=\"color: #888888;\"> * Date: October 8, 2018<\/span>\r\n<span style=\"color: #888888;\"> * Usage: Free        *\/<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FibonacciWithRecursion<\/span> {\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> n1 = <span style=\"color: #6600ee; font-weight: bold;\">0<\/span>;\r\n   <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> n2 = <span style=\"color: #6600ee; font-weight: bold;\">1<\/span>;\r\n   <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> n3 = <span style=\"color: #6600ee; font-weight: bold;\">0<\/span>;\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">displayFibonacci<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> count) {\r\n      <span style=\"color: #888888;\">\/\/Fibonacci Numbers: 0, 1, 1, 2, 3, 5, 8, 13<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">if<\/span>(count &gt; <span style=\"color: #6600ee; font-weight: bold;\">0<\/span>) {\r\n        n3 = n1 + n2;\r\n        n1 = n2;\r\n        n2 = n3;\r\n        System.<span style=\"color: #008800; font-weight: bold;\">out<\/span>.print(<span style=\"background-color: #fff0f0;\">\" \"<\/span> + n3);\r\n        displayFibonacci(count-<span style=\"color: #6600ee; font-weight: bold;\">1<\/span>);\r\n      }\r\n   }\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>(String args[])\r\n   {\r\n     <span style=\"color: #333399; font-weight: bold;\">int<\/span> count = <span style=\"color: #6600ee; font-weight: bold;\">10<\/span>;\r\n     System.<span style=\"color: #008800; font-weight: bold;\">out<\/span>.print(n1 + <span style=\"background-color: #fff0f0;\">\" \"<\/span> + n2); <span style=\"color: #888888;\">\/\/display 0 and 1<\/span>\r\n     displayFibonacci(count-<span style=\"color: #6600ee; font-weight: bold;\">2<\/span>); <span style=\"color: #888888;\">\/\/beginning from the 3rd <\/span>\r\n     number\r\n   }\r\n}\r\n<\/pre>\n<p>Feel free to copy this programs for free and run them to see how it works. You could change the value of the count to a larger number to view more numbers in the Fibonacci series.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fibonacci Numbers or Fibonacci Series is a set of numbers where the next number in the series is the sum of the preceding two numbers. &hellip; <\/p>\n","protected":false},"author":2,"featured_media":455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[85],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/29"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions\/456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/455"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}