{"id":111976,"date":"2026-05-26T12:57:42","date_gmt":"2026-05-26T07:27:42","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=111976"},"modified":"2026-05-26T12:57:43","modified_gmt":"2026-05-26T07:27:43","slug":"fibonacci-series-in-java","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/fibonacci-series-in-java\/","title":{"rendered":"Fibonacci Series in Java"},"content":{"rendered":"\n<p>The <strong>Fibonacci Series<\/strong> is one of the most popular <strong>number patterns<\/strong> used in <strong>programming<\/strong> and <strong>mathematics<\/strong>. Many beginners learn it when starting with <strong>Java<\/strong> because it helps them understand <strong>loops<\/strong>, <strong>logic<\/strong>, and <strong>number operations<\/strong> in a simple way.<\/p>\n\n\n\n<p>In <strong>Java<\/strong>, the <strong>Fibonacci Series<\/strong> is often used as a basic coding example for <strong>practice<\/strong> and <strong>problem-solving<\/strong>. It is a common topic in <strong>coding interviews<\/strong>, <strong>assignments<\/strong>, and <strong>beginner-level programs<\/strong>, making it an important concept for every <strong>Java learner<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>This blog helps you understand the <strong>Fibonacci Series in Java<\/strong> with simple examples and easy explanations for every approach.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>You will learn different ways to solve the <strong>Fibonacci Series<\/strong>, including <strong>Iteration<\/strong>, <strong>Recursion<\/strong>, and <strong>Memoization<\/strong>, step by step.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>By going through the code and explanations, beginners can improve their <strong>Java programming logic<\/strong> and problem-solving skills with greater confidence.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n  <strong style=\"font-size: 22px; color: #ffffff;\">\ud83d\udca1 Did You Know?<\/strong> <br \/><br \/>\n  <span>\n    The <strong style=\"color: #110053;\">Fibonacci Sequence<\/strong> was introduced by the <i>Italian mathematician <\/i>\n    <strong style=\"color: #110053;\">Leonardo of Pisa<\/strong> in the year \n    <strong style=\"color: #110053;\">1202<\/strong>.\n  <\/span>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Fibonacci Series: Definition<\/strong><\/h2>\n\n\n\n<p>The <strong>Fibonacci Series<\/strong> is a number pattern in which <strong><em>each new number is the sum of the previous two<\/em><\/strong>. It usually starts with 0 and 1, and continues like 0, 1, 1, 2, 3, 5, 8, and so on.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><em>Example:<\/em><\/strong><\/h3>\n\n\n\n<p><strong>Fibonacci Series: <\/strong>0, 1, 1, 2, 3, 5, 8&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><em>How it works:<\/em><\/strong><\/h3>\n\n\n\n<p>Here, 0 + 1 = 1<\/p>\n\n\n\n<p>Then, 1 + 1 = 2<\/p>\n\n\n\n<p>After that, 1 + 2 = 3<\/p>\n\n\n\n<p>In the same way, the series keeps continuing.<\/p>\n\n\n\n<p><strong>Also Read:<\/strong><a href=\"https:\/\/www.guvi.in\/blog\/array-data-structures-and-algorithms-in-java\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong> <em>Array Data Structures and Algorithms in Java<\/em><\/strong><\/a><\/p>\n\n\n\n<p>Want to level up your Java skills beyond basic programs? Join <strong>HCL GUVI&#8217;s<\/strong><a href=\"https:\/\/www.guvi.in\/courses\/programming\/java-programming\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=Fibonnaci+Series+in+Java\" target=\"_blank\" rel=\"noreferrer noopener\"><strong> Java Programming course<\/strong><\/a> and learn everything from core Java concepts to building real-world scalable applications, cracking interviews, and coding like a pro with industry-ready skills.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Different Approaches to Solve the Fibonacci Series in Java<\/strong><\/h2>\n\n\n\n<p>Here are different approaches for solving the<strong> <\/strong>Fibonacci Series in<strong> <a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a><\/strong>, each using a different logic and method of implementation:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>A. Iteration (Loop)<\/strong><\/h3>\n\n\n\n<p>The <strong>loop approach<\/strong> builds the <strong>Fibonacci series<\/strong> step by step using a <strong>for loop<\/strong>. It begins with <strong>0 and 1<\/strong>, then<em> keeps adding the last two values to form the next number<\/em>. After each step, values are <strong>updated and shifted forward<\/strong>, and the process continues until the required count is reached.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Code<\/strong><\/h4>\n\n\n\n<p>public class FibonacciIteration {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int n = 10;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int first = 0, second = 1;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; n; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(first + &#8221; &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int next = first + second;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first = second;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;second = next;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Input: <\/strong>n = 10<\/p>\n\n\n\n<p><strong>Output: <\/strong>0 1 1 2 3 5 8 13 21 34<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Code Explanation:<\/em><\/strong><\/h4>\n\n\n\n<ul>\n<li>In this code, we set <strong>n = 10<\/strong>, and start with <strong>first = 0<\/strong> and <strong>second = 1<\/strong>. A <strong>for loop<\/strong> runs from 0 to n-1, and in each step, it prints <strong>first<\/strong>.<\/li>\n\n\n\n<li>Then it calculates the next value using <strong>next = first + second<\/strong>, and updates the variables by moving <strong>second \u2192 first<\/strong> and <strong>next \u2192 second<\/strong>.<\/li>\n\n\n\n<li>This keeps repeating, and the <strong>Fibonacci series<\/strong> is printed in order, step by step.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>B. Recursion<\/strong><\/h3>\n\n\n\n<p>The<a href=\"https:\/\/www.guvi.in\/blog\/recursion-algorithms-in-dsa\/\" target=\"_blank\" rel=\"noreferrer noopener\"> <strong>recursive approach<\/strong><\/a> solves the problem by a <strong>function calling itself<\/strong>. It splits the task into smaller parts, <em>finds the previous two numbers first, and then adds them to get the result<\/em>. This continues until it reaches <strong>0 or 1<\/strong>, and then the final sequence is formed on the return.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Code<\/strong><\/h4>\n\n\n\n<p>public class FibonacciRecursion {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;static int fibonacci(int n) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (n &lt;= 1) return n;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return fibonacci(n &#8211; 1) + fibonacci(n &#8211; 2);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int n = 10;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; n; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(fibonacci(i) + &#8221; &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Input: <\/strong>n = 7<\/p>\n\n\n\n<p><strong>Output: <\/strong>0 1 1 2 3 5 8<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Code Explanation:<\/em><\/strong><\/h4>\n\n\n\n<ul>\n<li>Here, a <strong>function calls itself<\/strong> to find Fibonacci numbers.<\/li>\n\n\n\n<li>If <strong>n is 0 or 1<\/strong>, it returns that value directly. Otherwise, it calls itself using <strong>fibonacci(n-1)<\/strong> and <strong>fibonacci(n-2)<\/strong>, adds both results, and returns the answer.<\/li>\n\n\n\n<li>In the main loop, <strong>each value from 0 to n-1 is passed into the function<\/strong>, and the result is printed to form the series.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C. Memoization<\/strong><\/h3>\n\n\n\n<p>The <strong>array-based approach<\/strong> stores values and builds the <strong>Fibonacci series<\/strong> one step at a time. It starts with <strong>0 and 1<\/strong>, then <em>each new number is calculated using the previous two stored values<\/em>. Since results are saved, it avoids repeated work and quickly generates the full sequence.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Code<\/strong><\/h4>\n\n\n\n<p>public class FibonacciMemoization {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int n = 10;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int[] fib = new int[n];<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fib[0] = 0;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fib[1] = 1;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 2; i &lt; n; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fib[i] = fib[i &#8211; 1] + fib[i &#8211; 2];<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; n; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(fib[i] + &#8221; &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Input:<\/strong> n = 8<\/p>\n\n\n\n<p><strong>Output:<\/strong> 0 1 1 2 3 5 8 13<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Code Explanation:<\/em><\/strong><\/h4>\n\n\n\n<ul>\n<li>This code uses an <strong>array fib[]<\/strong> to store values.<\/li>\n\n\n\n<li>First, <strong>fib[0] = 0<\/strong> and <strong>fib[1] = 1<\/strong> are set. Then a loop runs from index 2 to n-1, where each value is calculated using <strong>fib[i] = fib[i-1] + fib[i-2]<\/strong>.<\/li>\n\n\n\n<li>After filling the array, another loop prints all values.<\/li>\n<\/ul>\n\n\n\n<p>If you want to explore more algorithms like the <strong>Fibonacci Series<\/strong> and improve your coding skills, check out <strong>HCL GUVI&#8217;s<\/strong><a href=\"https:\/\/www.guvi.in\/courses\/programming\/dsa-using-java\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=Fibonnaci+Series+in+Java\" target=\"_blank\" rel=\"noreferrer noopener\"><strong> DSA using Java course<\/strong><\/a>. Learn important DSA concepts with hands-on practice and become more confident in coding and interviews.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>The <strong>Fibonacci Series<\/strong> is more than just a beginner coding problem. It is one of those small concepts that quietly improve the way you understand <em>patterns, logic, and coding flow<\/em> in <strong>Java<\/strong>. Once you get comfortable with problems like this, coding starts feeling less confusing and more like connecting ideas step by step.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1779629835650\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Why do beginners struggle with the Fibonacci Series at first?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Most beginners get confused about how the previous two numbers keep changing after every step.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1779629837056\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Which Fibonacci approach is easier to understand for beginners?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The loop-based approach feels simpler because the flow is easier to follow step by step.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1779629837825\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Why is recursion slower in the Fibonacci Series?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Recursion repeatedly performs the same calculations, increasing execution time.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1779629838728\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>When should memoisation be used in Fibonacci programs?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Memoisation is useful when you want faster performance for larger Fibonacci numbers.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1779629876523\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Why do companies ask Fibonacci questions in coding interviews?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It helps interviewers assess how well you understand logic, loops, recursion, and problem-solving.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1779629878242\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>What is the biggest mistake while writing a Fibonacci program in Java?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Many learners forget to update the previous numbers correctly inside the logic.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The Fibonacci Series is one of the most popular number patterns used in programming and mathematics. Many beginners learn it when starting with Java because it helps them understand loops, logic, and number operations in a simple way. In Java, the Fibonacci Series is often used as a basic coding example for practice and problem-solving. [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":112259,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[720],"tags":[],"views":"29","authorinfo":{"name":"Abhishek Pati","url":"https:\/\/www.guvi.in\/blog\/author\/abhishek-pati\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/05\/Fibonacci-Series-300x116.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/05\/Fibonacci-Series.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/111976"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=111976"}],"version-history":[{"count":5,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/111976\/revisions"}],"predecessor-version":[{"id":112261,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/111976\/revisions\/112261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/112259"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=111976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=111976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=111976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}