Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: For Loop for Fibonacci code

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For Loop for Fibonacci code

    Hello Everyone,

    I need to write a For Loop that prints out the first 12 Fibonacci numbers:
    1 1 2 3 5 8 13 21 34 55 89 144.

    The problem I am having is that I can not get the first two 1 numbers. I only get 1.2.3.5.8.....
    int a=1;
    int b=1;
    for (int i=1; i<12;i++)
    {
      System.out.print(a+" ");
      a=a+b;
      b=a-b;
    }

    Do I need to add another For Loop that sub-tracks so that I can get the first digit of 1?


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: For Loop for Fibonacci code

    0,1,1,2,3,5,8....

    I think it works if you change b to 0.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: For Loop for Fibonacci code

    Whether the suggested approach is right or not, it should give you something to think about that will help you. Your current algorithm is unnecessarily complicated.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For Loop for Fibonacci code

    Thanks Hamenopi,

    Changing B to =0 worked.

Similar Threads

  1. Fibonacci Sequence Code in Java
    By dvmsandiego in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2014, 01:23 AM
  2. [SOLVED] Need help with YES/NO Loop in Fibonacci Sequence
    By LooneyTunerIan in forum Java Theory & Questions
    Replies: 11
    Last Post: March 24th, 2014, 03:06 PM
  3. [SOLVED] Fibonacci Help!
    By GoPredsGo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 16th, 2013, 06:47 PM
  4. java code for fibonacci series?
    By javawreker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 1st, 2012, 08:02 AM
  5. Fibonacci series
    By seanman in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 11th, 2011, 12:32 PM