can anyone explain me fibonacci number program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahmee
    New Member
    • Mar 2010
    • 39

    #1

    can anyone explain me fibonacci number program?

    f[0] = 1;
    f[1] = 1;
    int x;

    System.out.prin t(f[0] + "\n" + f[1] + "\n");
    for(x=2; x<=45; x++)

    f[x] = f[x-1] + f[x-2];
    System.out.prin tln(f[x]); [/CODE]
    i cant understand that how it hows after for statement.it first print 1,1 and then 2.the thing emm not getting is that how it print 2.iknown that in fibonacci number next number is the sum of previous two number but didnt get that how it print 2 and then 5 and so on.please let me explain.
    Last edited by Niheel; Jul 29 '10, 07:52 PM.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Just trace your program (and future programs) in a debugger, then you can see easily how it works!

    f[x] = f[x-1] + f[x-2];
    Now let x=2, then from this fact there follows by simply replacing letter x with 2:
    f[2] = f[1] + f[0];
    which is:
    f[2] = 1 + 1 = 2;
    because you defined
    f[1]and f[0] as being =1!

    Now do the same for x=3, then for x=4 etc. and you see how it works.

    Comment

    • ahmee
      New Member
      • Mar 2010
      • 39

      #3
      igot all but i didnt understand that when you take x=4 why it give all zero as output.Is reason is this that we didnt find x=2,x=3 first and directly jumps on the x=4 so when
      f[4]=f[4-1]+f[4-2]
      f[4]=f[3]+f[2]


      so there is no value regarding f[3]and f[4] so thats why it is displaying all zeros in the output


      thanks in advance

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Yes,that's the reason.

        You must first compute f[3] and f[2] and assign a value for both before you go on to compute f[4] !

        You can compute these values in a loop or through recursion. If you computed right, you will have then as a result stored in f[2]=2 and f[3]=3.
        You got the formula for computing f[4] right, so then f[4]=f[3]+f[2] = 3+2 = 5

        Comment

        • ahmee
          New Member
          • Mar 2010
          • 39

          #5
          i got q new question:

          i used math class methods pow for squaring the value and i used loop to square numbers upto ten.... but i am getting the problem.. here is my code bel0w

          for(int i=1,j=1; i<10&&j<10; i++,j++){

          System.out.prin t(Math.pow(i,j) );
          System.out.prin t("\n");


          and iam getting this output instead of squaring the numbers

          1.0
          4.0
          27.0
          256.0
          3125.0
          46656.0
          823543.0
          1.6777216E7

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            Dear Ahmee, please use a new thread if you have a new question.
            If you put it in its own thread instead of appending it to a non-related theme, other people will find it easily.
            Also please don't email me privately with additional questions. Also open a new thread for them. I am not responding to private questions, because I am not your private tutor, but will only provide help for everyone for free, that means any problem you have that will be discussed openly will also help others that have the same problem.

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              If you want to square numbers, why do you have j in your for loop?

              Instead use:
              Code:
              for(int i=1; i<10; i++){
              System.out.print(Math.pow(i,2));
              System.out.print("\n");
              What you had was:
              1^1
              2^2
              3^3
              ...

              Instead of:
              1^2
              2^2
              3^2
              ...

              Comment

              Working...