Write Java Program to Print Fibonacci Series upto N Number

Fibonacci Series Example crunchify.co  Write Java Program to Print Fibonacci Series upto N Number

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

cabe91689f6a1af616ace02827c6e89c Write Java Program to Print Fibonacci Series upto N Number

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

0cebc512d9a3ac497eda6f10203f792e Write Java Program to Print Fibonacci Series upto N Number

with seed values

a92c5f0981136ba333124cdfe6d3c3ce Write Java Program to Print Fibonacci Series upto N Number

Here is a simplest Java Program to generate Fibonacci Series.

Output:

Have anything to add to this article? Please chime in and join the conversion.
twitter Write Java Program to Print Fibonacci Series upto N Numberfacebook Write Java Program to Print Fibonacci Series upto N Numbergoogle Write Java Program to Print Fibonacci Series upto N Numberpinterest Write Java Program to Print Fibonacci Series upto N Numbertumblr Write Java Program to Print Fibonacci Series upto N Numberbuffer Write Java Program to Print Fibonacci Series upto N Number

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/ Arpit

      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/ Arpit

      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/ Arpit

      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/ Arpit

      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;
           while(count<=n){
          
               c=b+a;
          System.out.print(c);
           a=b;
           b=c;
            
           count++;}
            }
        }
    
    • http://crunchify.com Arpit 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 Arpit 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/ Arpit Shah

      It’s syntax highlighter plugin.

  • HHHProgram

    Hey Arpit 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/ Arpit Shah

      Thanks much for your comment.