Write Java Program to Print Fibonacci Series upto N Number

Fibonacci Series Example - crunchify.co

In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:  

0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\;

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

F_n=F_{n-1} + F_{n-2},\!\,

with seed values

F_0=0,\; F_1=1.

Here is a simplest Java Program to generate Fibonacci Series.


Have anything to add to this article? Please chime in and join the conversion.
Tweet about this on TwitterShare on FacebookShare on Google+Pin on PinterestShare on TumblrBuffer this page

Enjoyed this post?

Be sure to subscribe to the Crunchify newsletter and get regular updates about awesome posts just like this one and more! Join more than 8000 subscribers!

  • Koko

    How can I get the sum of all the fibonacci sequence produced?

    • http://crunchify.com/ a.shah

      Will update above code soon with that option.. :)

  • Smirnoff

    you don’t need a third variable

  • mrphi

    What is the plugin for source code ?

    • http://crunchify.com/ a.shah

      I’m using Crayon Syntax Highlighter.

  • Neel Sheyal

    We can reduce the time complexity from exponential to linear by not performing the redundant work of computing Fibonnaci values many times from scratch. We can use the concept of dynamic programming by storing the Fibonacci numbers calculated so far.

    • http://crunchify.com/ a.shah

      Thanks Neel. Could you share your code?

  • kaustab ganguly

    why c=1,b=1,a=0

  • umika

    Hi, I would like whether this method can apply for user number in unordered list and the output will come out in order list and in range? For example the number enter is 2,5,1,4,6,3,8 then the result is number less than 5 are 1,2,3,4 and number less than 10 are 5,6,8

  • Haile Hadaro

    Thank U!

    • http://crunchify.com/ a.shah

      You are welcome.

  • Rabia

    there is another and shorter way of doing this program.

    public class JavaApplication11 {
         * @param args the command line arguments
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
           System.out.print("enter n");
           int n=input.nextInt();
           int count=1;
            int c=1,b=1,a=0;
    • http://crunchify.com a.shah

      Thanks for posting. Just Print Number 1 Before while loop and you are all set.

    • Dixus

      check this out:
      class F{public static void main(String[] a){int f=0; int t=1;while(f<Long.parseLong(a[0])){System.out.println(f);t=f+(f=t);}}}

      fibonacci in 127 characters :D

    • asdf

      also change:”System.out.print(c);”

      to:”System.out.print(c + “,”);”

  • Tracy

    Thanks for explaining this…one question though, why is i=3 in the for statement of the Loop program?

    • http://crunchify.com a.shah

      Because 1st two digits are number 1.

      if(number == 1 || number == 2){
                  return 1;
  • Dark

    thanks for sharing. What environment it is? looks colorful and attractive :D

    • http://crunchify.com/ a.shah

      It’s syntax highlighter plugin.

  • HHHProgram

    Hey a.shah,
    Excellent article for the Fibonacci series of course this blog is doing a very good job of serving useful information. I’m proud to be a part of its Readers community.
    For the Fibonacci programs in different languange like C language,JAVA,C# must visit hhhprogram

    • http://crunchify.com/ a.shah

      Thanks much for your comment.