Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calculate Fibonacci sequence  RSS feed

 
Desiree Bernard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

I am a beginner and I need to write a program that calculates the Fibonacci sequence until a certain maximum.

The code works only I have a fencepost issue with the commas. I need to have a comma after every printed entry

except the last. Does anyone have any ideas as to a solution?


__________________________________
       
        println ("This program lists the Fibonacci sequence.");
        int maxValue = readInt ("Max value? ");
        int term1 = 0;
        int term2 = 1;
       
        while (term1 < maxValue) {
        int term3 = term1 + term2;
        print (", " + term1 );    
        term1 = term2;
        term2 = term3;
       
    }
    }
}
 
Pete Letkeman
Ranch Hand
Posts: 184
8
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things here:
  • Please post the full code, "print" by itself does not work unless you have imported a package or two.
  • Please format your code with the "code" tags in this editor.
  • Please provide the output that you are currently receiving.
  • Please provide the output that you would like to receive.
  •  
    Norm Radder
    Rancher
    Posts: 2052
    26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
      ideas as to a solution?

    print the first one without a comma
    print all following ones with a leading comma
     
    Desiree Bernard
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello,

    Thank you for your replies.

    1. Please find below the output versus expected - you will see the comma problem.

    2. I tried printing one number before the loop with the comma afterwards and this screwed up the calculations.

    _____________________________________

    test #1:run() #1
    expected output:This program lists the Fibonacci sequence.
    Max value? 10000
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
    your output:This program lists the Fibonacci sequence.
    Max value? 10000
    , 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
    differences:3c3
    < 0,·1,·1,·2,·3,·5,·8,·13,·21,·34,·55,·89,·144,·233,·377,·610,·987,·1597,·2584,·4181,·6765
    > ,·0,·1,·1,·2,·3,·5,·8,·13,·21,·34,·55,·89,·144,·233,·377,·610,·987,·1597,·2584,·4181,·6765
    result: fail
     
    Norm Radder
    Rancher
    Posts: 2052
    26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please post the code wrapped in code tags.  Also post the program's output.
     
    mir basheer ali
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

            int maxValue = //Value entered by the user;
            int term1 = 0;
            int term2 = 1;
           //Print the first two elements
            System.out.print(term1+","+term2);
            while(term2 < maxValue){
                int term3 =term1 + term2;
                //Print the sum prepended by comma.
                System.out.print(","+term3);
                term1 = term2;
                term2 = term3;
               
            }
    This code eliminates the issue of the comma appearing after the last elements. As the first two elements of the fibonacci sequence is fixed print them first and prepend a comma before each subsequent element is printed.
     
    Desiree Bernard
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you!
     
    Knute Snortum
    Sheriff
    Posts: 3842
    91
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mir basheer ali: Thank you for your post, and Welcome to the CodeRanch.

    A couple of notes on your post: In the Beginning Java forum, we frown upon posting "complete solutions", that is, posting code that can be copied and pasted into the OP's program.  The reason for this is that we want the OP to come to their own conclusions and discover solutions for themselves, so we give hints and guide them but don't write code for them.

    It's okay to post code when the OP has already posted a solution, or if your code is a snippet that illustrates an idea you want to get across.   Your post is close to a complete solution -- it's a bit of a judgement call.

    The second thing is that if you UseCodeTags (that's a link), your code will look much better.

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!