BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Display Fibonacci Series using loops

Last Updated: September 15, 2017 by Chaitanya Singh | Filed Under: Java Examples

The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Here we will write three programs to print fibonacci series 1) using for loop 2) using while loop 3) based on the number entered by user

To understand these programs, you should have the knowledge of for loop and while loop.

If you are new to java, refer this java programming tutorial to start learning from basics.

Example 1: Program to print fibonacci series using for loop

public class JavaExample {

    public static void main(String[] args) {

        int count = 7, num1 = 0, num2 = 1;
        System.out.print("Fibonacci Series of "+count+" numbers:");

        for (int i = 1; i <= count; ++i)
        {
            System.out.print(num1+" ");

            /* On each iteration, we are assigning second number
             * to the first number and assigning the sum of last two
             * numbers to the second number
             */
            int sumOfPrevTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfPrevTwo;
        }
    }
}

Output:

Fibonacci Series of 7 numbers:0 1 1 2 3 5 8

Example 2: Displaying Fibonacci Sequence using while loop

public class JavaExample {

    public static void main(String[] args) {

        int count = 7, num1 = 0, num2 = 1;
        System.out.print("Fibonacci Series of "+count+" numbers:");

        int i=1;
        while(i<=count)
        {
            System.out.print(num1+" ");
            int sumOfPrevTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfPrevTwo;
            i++;
        }
    }
}

Output:

Fibonacci Series of 7 numbers:0 1 1 2 3 5 8

Example 3: Program to display the fibonacci series based on the user input

This program display the sequence based on the number entered by user. For example – if user enters 10 then this program displays the series of 10 numbers.

import java.util.Scanner;
public class JavaExample {

    public static void main(String[] args) {

        int count, num1 = 0, num2 = 1;
        System.out.println("How may numbers you want in the sequence:");
        Scanner scanner = new Scanner(System.in);
        count = scanner.nextInt();
        scanner.close();
        System.out.print("Fibonacci Series of "+count+" numbers:");

        int i=1;
        while(i<=count)
        {
            System.out.print(num1+" ");
            int sumOfPrevTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfPrevTwo;
            i++;
        }
    }
}

Output:

How may numbers you want in the sequence:
6
Fibonacci Series of 6 numbers:0 1 1 2 3 5

Checkout these related Java programs:

  1. Java Program to Find Factorial
  2. Java Program to check prime number

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to Add two Binary Numbers
  3. Java Program to find largest of three numbers using Ternary Operator
  4. Java Program to check if Number is Positive or Negative
  5. Java Program to find the Smallest of three numbers using Ternary Operator

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Amy says

    May 26, 2018 at 5:05 PM

    Need the same for first 10 numbers of Fibonacci series

    Reply
  2. Afzal Hamdulay says

    August 13, 2018 at 3:05 PM

    Why we have used the value of i=1 and not i=0 and what will happen if we take i=0?

    Reply
    • Raja says

      August 19, 2018 at 7:17 PM

      if you take i = 0 it also right but in case of that –
      case 1. in for loop for loop if you take i = 0; you have to take the for loop till count not equals to.
      syntax : for(i = 0; i < count; ++i){}
      case 2. in case of while loop also –
      syntax : int i = 0;
      while(i < count){}

      I hope it would be helpful for you. taking input as 0 is not wrong but in that case the loop goes upto the previous number of the limit but doesn't upto the number.

      Reply
    • K.M.T says

      October 11, 2018 at 6:07 PM

      Because if we start from i=0, the program executes 1 more time than required. Just count from 0 to a specific no. and from 1 to a specific no. You will find the difference

      Reply
    • Bagaskara Wisnu Gunawan says

      October 23, 2018 at 4:55 PM

      Let’s use example 1 as a test case. When you assign i=0 for the loop, it will then loop from 0 to 7 which is 8 times (0, 1, 2, 3, 4, 5, 6, 7), so we need to assign i=1 and the looping count is right.

      Reply
      • Likitha says

        November 25, 2018 at 4:28 AM

        Good and smart🙂

        Reply
      • Andrew says

        March 11, 2020 at 9:35 AM

        if you are to use i=0 then in the while loop you put i<count

        Reply
  3. Naseer ullah says

    October 24, 2018 at 6:44 AM

    Because Fibonacci series is start from 0.

    Reply
  4. StigaICE says

    March 20, 2019 at 1:48 PM

    What does it mean to have a blank string in System.out.println(” “);
    From solution 1, System.out.println(num1 + ” “);

    Reply
  5. Abishek M says

    June 26, 2019 at 2:43 PM

    THE entire code is being printed in the Run Time Window. So, please make the necessary corrections and publish the same again ! Appreciate the effort.

    Reply
  6. joe says

    December 19, 2019 at 12:33 AM

    It really bothers me why i cannot figure out where the second ‘1’ in the 0 ,1 ,1 …. sequence comes from within the code

    Reply
  7. Pidva says

    January 22, 2020 at 12:33 PM

    Why use sumOfPrevTwo ?

    Reply
  8. Vigneshram says

    February 9, 2020 at 2:01 AM

    We can use i=0 and i<7
    looping is also correct for me it from 0 to 6(7 values)

    Reply
  9. anonymous says

    May 12, 2020 at 5:40 PM

    Program to display the fibonacci series based on the user input and its sum??

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap